Raunak Gupta
Raunak Gupta

Reputation: 10809

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.

Upvotes: 6

Views: 6082

Answers (1)

NatraxYN
NatraxYN

Reputation: 109

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.

Upvotes: 5

Related Questions