Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to validate a field when another field is changed #23

Open
aranoe opened this issue Mar 25, 2020 · 2 comments
Open

Add option to validate a field when another field is changed #23

aranoe opened this issue Mar 25, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@aranoe
Copy link
Collaborator

aranoe commented Mar 25, 2020

Problem Statement

Sometimes it is required to immediately validate a field if the value of another field has been changed. E.g. having two date fields, where one must be before/after the other, it is desired to validate one date based on the others current value.

Prefered API

Option 1

Validate a field, if the value of a specific other field changed:

createForm()({
  fromDate: field.date().validateOnOtherFieldChange("toDate"),
  toDate: field.date().validateOnOtherFieldChange("fromDate")
})

In this case it could also be desried to target multiple other fields. One way is to chain the validation triggers, the other one is to pass an array instead of a single value:

createForm()({
  // chain
  fromDate: field.date().validateOnOtherFieldChange("toDate")
                        .validateOnOtherFieldChange("anotherDate"),
  // pass array
  toDate: field.date().validateOnOtherFieldsChange(["fromDate", "anotherDate"]),
  anotherDate: field.date();
})

Option 2

Validate a field, if any value in the form changed:

createForm()({
  fromDate: field.date().validateOnAnyFieldChange(),
  toDate: field.date().validateOnAnyFieldChange()
})

It might even make sense to implement both Options.

@aranoe aranoe added the enhancement New feature or request label Mar 25, 2020
@ysfaran ysfaran self-assigned this Mar 27, 2020
@ysfaran
Copy link
Owner

ysfaran commented Mar 27, 2020

Option 2 should be a sperate ticket.

For Option 1 it might be better to move this feature from field level configuration to form level configuration:

  • field level configuration should not be dependent on other fields
  • this could also cause more complexity in the future (code and api wise)

Adapted approach for form level configuration

createForm()({
  fromDate: field.date(),
  toDate: field.date(),
  // ...
}).withValidationTriggers({
  fromDate: "toDate",
  toDate: ["firstField", "secondField"]
});

But this doesn't look intuitive enough and on top of that it is not really fluent. It might be better to use a more generic approach, where triggers can be anything:

config.withTriggers({
  fromDate: triggers()
    .validationOnChangeFor("toDate")
    .resetForReturnedFields(value => (value.getUTCFullYear() > 2010? "toDate" : undefined)),
});

(readout as: "fromDate triggers validation on change for toDate"
or:

config.withTriggers({
  toDate: triggers([
    onChange().validationFor("fromDate"),
    onBlur().resettingOfReturnedFields(value => (value.getUTCFullYear() > 2010? "anyField" : undefined))
  ])
});

@ysfaran
Copy link
Owner

ysfaran commented Mar 31, 2020

useFluentForm now returns validateField and validateAllFields, so this behaviour can be implemented by hand:

const { values, touched, validateField } = useFluentForm(config);

useEffect(() => {
  if(touched.toDate) {
    validateField("toDate");
  }
},[values.fromDate])

But I'll keep the ticket open for now, since it might be worth implementing triggers directly in react-fluen-form, because the solution above might not be straighforward. Also it could get quite complicated for the users to implement if the form is really big and has a lot of inter-field-dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants