I have validation in Laravel 10 that contains validation for company_description. I want it to be required when two other fields have specified values. I got this code:
'company_description' => [
'string',
'nullable',
function ($attribute, $value, $fail) {
if (request()->input('offer_type_id') == 1 && request()->input('use_default_company_description') == false) {
if (empty($value) || $value == '') {
$fail('Company description is required.');
}
}
},
],
but its not working. If nullable is removed its working, but I need this field to be nullable. What am I doing wrong?
empty($value) || $value == '', which is whatnullablehandles, so your Rules overlap.$fail()code can never be triggered if you keepnullable, since you're checking forempty($value) || $value == '', whichnullablealready handles... You need to removenullableand handle it manually in your rule, probably as anelse { ... }statement.