Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add laravel multiple required_if (like AND Logical)

How to add laravel multiple required_if (like AND Logical) at Laravel Validation. I want to required 'featured_img' if (pagetype == '2' and featured == '1')

I try this one :

'featured_img'   => 'required_if:pagetype,2|required_if:featured,1|mimes:jpg,jpeg,bmp,png',

but this validation make 'featured_img' required though one of them false (it is like logical "OR". i want make it like "AND" logical)

Thanks

like image 974
Nugraha Avatar asked Jan 02 '16 09:01

Nugraha


1 Answers

You can use custom validation like :

$validator->sometimes('featured_img', 'required', function($request) {
    return ($request->pagetype == '2' && $request->featured == '1')
});

Take a look at Conditionally Adding Rules.

Hope this will help.

like image 91
Zakaria Acharki Avatar answered Nov 01 '22 23:11

Zakaria Acharki