Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

required_without not working with multiple fields

Laravel required_without is not working when I'm passing multiple fields.

This is my rules:

$rules = [
    'startDate' => 'date|date_format:m/d/Y|required_without:customerId,purchaseId,orderId',
    'endDate' => 'date|date_format:m/d/Y|required_without:customerId,purchaseId,orderId',
];

What I want, when I pass customerId or purchaseId or orderId (but not all) then I shouldn't be getting any error. But it is giving me any error that startDate is required.

Any assistance will be highly appreciated.

like image 969
Raunak Gupta Avatar asked Jan 12 '18 13:01

Raunak Gupta


1 Answers

I believe, the rule you are looking for is required_without_all IE. the startDate field must be present when all of the other specified fields (customerId,purchaseId,orderId) are not present.

As per https://laravel.com/docs/5.6/validation

required_without_all:foo,bar,... The field under validation must be present and not empty only when all of the other specified fields are not present.

In contrast to

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

like image 182
NatraxYN Avatar answered Sep 22 '22 16:09

NatraxYN