What's is the difference between required_with and required_with_all laravel validation
Asked Answered
K

2

10

I already tried have a look at https://laravel.com/docs/5.4/validation but still , i don't really get what's is the difference between :

required_with_all

and

required_without


Anyone can explain to me in detail what's the difference ?

Kenwee answered 20/6, 2017 at 4:47 Comment(0)
R
12

required_with_all :

Laravel Doc: The field under validation must be present only if all of the other specified fields are present.

required_without_all :

Laravel Doc: The field under validation must be present and not empty only when all of the other specified fields are not present.

Example:

$rules = array(
    'facebook_id' => 'required_without_all:twitter_id,instagram_id',
    'twitter_id' => 'required_without_all:facebook_id,instagram_id',
    'instagram_id' => 'required_without_all:facebook_id,twitter_id',
);
$validator = Validator::make(Input::all(), $rules);

required_with:

Laravel Doc: The field under validation must be present only if any of the other specified fields are present.

Example:

$rules = array(
'sell' => 'required_without:rent',
'rent' => 'required_without:sell',
'price' => 'required_with:sell|numeric|min:0',
);
Related answered 20/6, 2017 at 5:13 Comment(0)
S
4

required_with:

The field under validation must be present and not empty only if any of the other specified fields are present.

required_with_all:

The field under validation must be present and not empty only if all of the other specified fields are present.

Note: Check bold text above.

For more detail see Laravel docs

Slatternly answered 20/6, 2017 at 4:49 Comment(2)
'other specified fields are present.' , base on this , fields are present means the request have that field and it doesn't matter whether that field is emtpy or have value?Kenwee
Please read carefully "must be present and not empty only" so value requiredSlatternly

© 2022 - 2024 — McMap. All rights reserved.