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

Validating against two or more fields in the fields.js file #180

Open
LaiTang-GDS opened this issue Jun 9, 2022 · 1 comment
Open

Validating against two or more fields in the fields.js file #180

LaiTang-GDS opened this issue Jun 9, 2022 · 1 comment

Comments

@LaiTang-GDS
Copy link

Is there a way to define a validation method that can refer to other fields?
In our app we need to validate the sum of characters entered for a combination of fields. We have a workaround where we do validation in a separate step and return the errors manually but was wondering if there was a better way of declaring this type of validation.

@HughePaul
Copy link
Contributor

There are other validators that look at other fields as part of their validation, eg match, or the dates validators beforeField and afterField. You could do something like:

 function combinedLength(value, length, otherFieldName) {
   const otherValue = this.values[otherFieldName];
   return validators.string(value) && validators.string(otherValue) &&
     ((value.length + otherValue.length) <= length);
}

and specify it with something like:

validator: [
  { fn: combinedLength, arguments: [ 200, 'otherFieldName' ] }
]

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

No branches or pull requests

2 participants