validatorjs icon indicating copy to clipboard operation
validatorjs copied to clipboard

required_if does not run in line with other validations

Open the-architect opened this issue 7 years ago • 8 comments

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: ''
    }

the-architect avatar Jul 10 '17 16:07 the-architect

Maybe I am just doing it wrong? If there is a better way to achieve this, let me know.

the-architect avatar Jul 10 '17 16:07 the-architect

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)

mikeerickson avatar Jul 10 '17 22:07 mikeerickson

@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.

iamdtang avatar Jul 10 '17 23:07 iamdtang

hmm, that's true. I am using mobx-react-form. I'll have a deep dive into the code tomorrow. Will report back.

the-architect avatar Jul 10 '17 23:07 the-architect

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 avatar Jul 11 '17 13:07 the-architect

@the-architect I am going to keep this ticket open for the meantime until I can verify the issue with required_if

mikeerickson avatar Jul 11 '17 17:07 mikeerickson

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.

the-architect avatar Jul 11 '17 17:07 the-architect

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'

Ephim64 avatar Jul 12 '17 13:07 Ephim64