vacuumlabs/react-custom-validation

Optional Fields

cickes opened this issue · 1 comments

How can we skip validating empty or null fields if they are optional while also validating empty or null fields on required fields?

@cickes, you just need to specify validation rule functions that correspond to the desired behavior. This library allows you to treat each field separately, so it is straightforward to make some fields required and other fields optional.

To completely skip validating optional fields, exclude these fields from the validation config. If you want to validate an optional field, but only if it is not empty or null, you need to provide validation rule functions that accept empty or null values as valid. For example here:

validations: {
      ...
      phoneNumber: [[isPhoneNumber, password, 6]],
      ...
}

you need to make sure that isPhoneNumber(null) returns true (or any other valid value, for more info see https://github.com/vacuumlabs/react-custom-validation#defining-custom-rules) for empty and null input values.

If you have any additional questions, please let me know!