0

Suppose I have form wizard, I want to separate validation rules based on the wizard index, suppose I want to have the following validation rules if wizard_index value is first or all or if wizard_index not exists.

$rules =  [
    'wizard_index' => ['required', 'string', 'in:first,second,third,all'],
    'name' => ['required_if:wizard_index,in:first,all', 'string', 'max:50', 'min:3'],
    'about' => ['required_if:wizard_index,in:first,all', 'string', 'max:500', 'min:10'],
    'size' => [
        'required_if:wizard_index,in:first,last', 'string',
        'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
    ]
];

Above validation rules not working, but if I remove in: and check for only one value it's working.

And the last point is I want to remove required rule from wizard_index, and add one other extra condition for other fields that should be required if wizard_index does not exist.

2
  • The second argument to the required_if rule is a value Commented Sep 24, 2019 at 10:40
  • @CaddyDZ Then is there a way to use it with OR operator? Commented Sep 24, 2019 at 10:43

1 Answer 1

1

Simply add separately those rules, something like this:

$rules =  [
  'wizard_index' => ['nullable', 'string', 'in:first,second,third,all'],
  'name' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:50', 'min:3'],
  'about' => ['required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string', 'max:500', 'min:10'],
  'size' => [
    'required_if:wizard_index,first', 'required_if:wizard_index,all', 'required_without:wizard_index', 'string',
    'in:0 - 1,2 - 10,11 - 50,51 - 200,201 - 500,"501 - 1,000","1,001 - 5,000","5,001 + more"'
  ]
];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.