How to use required_unless in laravel

Chintan7027 picture Chintan7027 · May 20, 2016 · Viewed 16.4k times · Source

I have gone through the validations in Laravel. I have taken so many validation rules from Laravel Validations Rules

I want to user required_unless rule for following conditions.

$rules = array(
        'facebookID' => 'alpha_num',
        'googleID' => 'alpha_num',
        'email' => 'required_unless:facebookID,null|required_unless:facebookID,null|email|max:32',
        'password' => 'required_unless:facebookID,""|required_unless:googleID,""|max:20',            
    );

I want to add validation rule of email and password is only required if signup with facebook or google is not attempted.

I believe we can use required_unless or required_if

Please help me to solve this problem.

Answer

Dat Pham picture Dat Pham · May 20, 2016

The laravel validation rule only accept value, that's mean required_unless:facebookID,null is evaluated as: the field is required unless facebookID='null'. So it's not a case you would need.

My suggest solution is using require_without_all:

'email' => 'required_without_all:facebookID,googleID',
'password' => 'required_without_all:facebookID,googleID'

The reference link for you: https://laravel.com/docs/5.1/validation#rule-required-without-all

Regards