validatorjs
validatorjs copied to clipboard
required_if does not run in line with other validations
Hi all,
I don't know if this is intended behaviour, but I need to have an email field validated if another field 'emailRequired' is eql to 'yes'. Default is 'no'.
While all other validations work properly this one does only kick in, when I blur on the email field:
{
name: 'emailRequired',
type: 'hidden'
},
{
name: 'email',
type: 'email',
pattern: '[^ @]*@[^ @]*',
placeholder: 'Email',
rules: 'required_if:emailRequired,yes',
value: ''
}
Maybe I am just doing it wrong? If there is a better way to achieve this, let me know.
Hey @the-architect I am going to have to dig deeper into this issue as I have not actually used this functionality personally. I have picked up project maintenance to help @skaterdav85 so I have not touched all the code (yet)
@the-architect So it isn't working only on the blur event? Seems like the validator is getting called at the wrong time then, since this library doesn't take into account any user events.
hmm, that's true. I am using mobx-react-form. I'll have a deep dive into the code tomorrow. Will report back.
Ok, to continue with my work I decided to create 2 separate forms, one without and one with the required email field. Works perfectly now, as expected. As far as the required_if bug is concerned, it would be nice to have a fix for it, but from my side, we can close this issue.
@the-architect I am going to keep this ticket open for the meantime until I can verify the issue with required_if
So basically the behaviour I saw was that isValid would return true even if the email field was empty. However, once the input field got a blur event it would trigger the required_if validation as well and isValid would correctly be false.
Hi. If i understand description correctly, we can specified a value that anotherfield should be equal to. Can we pass a boolean value to this rule?
'required_if:anotherfield,true'