Search code examples
laravellaravel-validation

validation two fileds in laravel validation


I have 2 fields to check validation...

parent
tag

I want something like this:

if(parent == 0)
     return 'tag is required';
else 
    return 'tag can be nullable';

I try something like this but it is wrong:

'parent' => 'nullable|numeric',
'tags' => 'required_with:parent=0|array|max:8',

Solution

  • Use the required_if rule.

    The field under validation must be present and not empty if the anotherfield field is equal to any value.

    'parent' => 'nullable|numeric',
    'tags' => 'required_if:parent,0|array|max:8',
    

    https://laravel.com/docs/5.5/validation#rule-required-if