I'm using custom messages in a validation with Laravel, most of them work for me but specifically the ones that are requiredIf don't work for me.
I've seen them used like this:
protected $mensajes = [
'salas.required_if' => 'mensaje personalizado',
'dormitorios.required_if' => 'mensaje personalizado'
]
but that doesn't work for me. I clarify that if it takes the data as mandatory when the condition is met, but it does not send the message that I defined, it sends one that I understand is predefined:
"The rooms field is mandatory"
"The bedrooms field is mandatory"
My condition is something like this:
$arreglo['salas'][] = Rule::requiredIf(function() use($request)
{
return $request->filled('dormitorios') ? false : true;
})
$arreglo['dormitorios'][] = Rule::requiredIf(function() use($request)
{
return $request->filled('salas') ? false : true;
})
To give a little more context as to what I want to do, I am receiving two arrangements in a request (living rooms, bedrooms), but I want at least one or both of them to always come, but if both are absent it would be wrong, and thus the validation of the requiredIf .
The solution I got was to put the message for required , like this:
I kept the validation the same:
That way it started to work fine, the validation is still fulfilled for 'rooms' and 'bedrooms' but this time the message is the one I defined. To me this behavior makes sense since the required and requiredIf validations don't make sense for them to be together since required would take precedence over requiredIf . The only thing I find strange is that Laravel by default already has messages for required_if in the validation.php files located in the resources/lang directory , but that message never appears, instead the message for required from the same file always appears.
The message "The field :attribute is required" that I mentioned in the statement of the problem, is found in the file resources/lang/en/validation.php .