DanFMoore / react-validatorjs-strategy

Strategy for using validatorjs with react-validation-mixin
The Unlicense
10 stars 5 forks source link

How to ensure at lease one of the two fields has proper value #10

Closed hongyuan1306 closed 7 years ago

hongyuan1306 commented 7 years ago

I have a form in which I want the user to input at least one of his mobile number and phone number.

I tried to add a custom validator like the one below:

Validator.register(
  'notbothempty',
  function(val, req) {
    const other = this.validator.input[req]
    return (val !== undefined && val !== null && val !== '') ||
      (other !== undefined && other !== null && other !== '')
  }
)

And create a schema like:

strategy.createSchema({
    mobile: 'mobile|notbothempty:phone',
    phone: 'phone'
})

This does not work however. With the fields empty, the custom validator is not invoked.

How can this sort of validation be performed?

hongyuan1306 commented 7 years ago

Sorry this question is more appropriate for validatorjs