Extending Validation with Custom Message Attribute
So I extended the validator and added my own rule (works great). It shows the custom message below but when I try and use :values or :other it just prints that out in text. :attribute works fine.
Just wondering how they're set or how to use them.
"empty_when" => "The :attribute must be blank when :other is entered.",
@usman Thanks, I've opted to extend the validator class so I can easily add more and use that function instead of a closure. I think I saw it at the bottom but didn't click in my head that that was it... :P
I have a (late) follow-up question to your solution, @bashy. I've defined my custom validation rule and replacer pretty much exactly like you did.
I'm trying to find a way to have the replacer use the attributes()-function of my form request class so that I can control how each of the items in $parameters are finally displayed in the returned error message.
Example: The empty_when-rule is set for 'library' and the :other-parameter could be 'book_id'. But I want the final message to say something like "The Library-name must be blank when Book is entered.'
The key here being it says 'Book' instead of 'book_id'. Since I've entered 'library_name' => 'Library-name' in the attributes()-method of my form request class, the validator automagically replaces this for me. Not so with 'book_id'. I haven't been able to figure out how to do this so far.
'emptyWith' => 'The :attribute field must be empty when :other is present.'
:attribute is replaced with 'Library-name'
:other is replaced with 'book_id'
I want my replacer-method to use the form request class' attributes()-method so that the parameters that replaces :other, in this case 'book_id' get pretty names like Book (similar to how :attribute is ends up as 'Library-name' and not 'library_name').
@effkay I don't know how so I'd just do a message for each field?
public function messages()
{
return [
"library_name.empty_when" => "The :attribute must be blank when Book is entered.",
"book_id.empty_when" => "The :attribute must be blank when Library-name is entered.",
];
}