Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use required_unless in laravel

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.

like image 367
Chintan7027 Avatar asked May 20 '16 08:05

Chintan7027


1 Answers

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

like image 79
Dat Pham Avatar answered Oct 01 '22 14:10

Dat Pham