I'm making one page in Laravel and validating using Form Request. I have 2 codes on that page.
I want to set conditions in such a way that these codes can be empty but when filled then only one is required.
I've tried this but not getting the proper results.
return [
'coupon_code' => 'nullable|required_without:promo_code',
'promo_code' => 'nullable|required_without:coupon_code'
];
From your description, it is clear you want to allow these three input pairs:
Enforcing any of those 3 pairs of values doesn't require any validation rules at all. You can set both rules to nullable, and be fine.
But reading between the lines, it seems you're asking for this value pair to be rejected:
If that's the case, then instead of using required
you would use prohibited_unless
:
[
'promo_code' => [
'prohibited_unless:coupon_code,null',
],
'coupon_code' => [
'prohibited_unless:promo_code,null',
],
]
This assumes you have the default ConvertEmptyStringsToNull
middleware in place.