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

required_if doesn't work as expected #434

Open
risanto opened this issue Aug 6, 2021 · 3 comments
Open

required_if doesn't work as expected #434

risanto opened this issue Aug 6, 2021 · 3 comments

Comments

@risanto
Copy link

risanto commented Aug 6, 2021

So, I set up these rules:

channel: 'required|numeric',
url: 'required_if:channel,1|url_validation',

And it's supposed to check for url when the channel is 1, but the problem is when I send this payload it passes just fine and doesn't trigger required or the subsequent url_validation (a custom validation).

"channel": 1,
"url": null,
@min-emcloud
Copy link

const Validator = require('validatorjs')

const data = {
  name: 'John',
  hasAllergy: true
};

const rules = {
  name: 'required',
  hasAllergy:  'required|boolean',
  allergicTo: 'required_if:hasAllergy,true'
}

const validation = new Validator(data, rules)
console.log('validation result', validation.passes()) // expected `false` but shows `true`

@hellohasan
Copy link

@risanto did you find the solution?

@AlessandroStaffolani
Copy link

As shown in this closed issue #252. You can solve it using the array/object notation. Here is a working example:

const Validator = require('validatorjs');

const data = {
  name: 'John',
  hasAllergy: true,
};

const rules = {
  name: ['required'],
  hasAllergy: ['required', 'boolean'],
  allergicTo: [{ required_if: ['hasAllergy', true] }],
};

const validation = new Validator(data, rules);
console.log('validation result', validation.passes()); // expected `false` and shows `false`

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

4 participants