Ali
Ali

Reputation: 1217

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',

Upvotes: 3

Views: 80

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

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

Upvotes: 3

Related Questions