required_without not working with multiple fields
Asked Answered
A

1

6

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.

Ambivalence answered 12/1, 2018 at 13:42 Comment(0)
H
5

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.

Harmaning answered 23/3, 2018 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.