0
votes

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.

1
The second argument to the required_if rule is a valueSalim Djerbouh
@CaddyDZ Then is there a way to use it with OR operator?jones

1 Answers

1
votes

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"'
  ]
];