Tuesday, February 28, 2017

Laravel validation rules - get the broken rule name

Scenario

I am developing a multi-language website in Laravel. The languages can be added / deleted from the Admin section. The Front End section contains various forms, validated by controllers using appropriate FormRequest objects.

Problem

The validation error messages are hard-coded in their corresponding language validation.php files, which I cannot use because, well, the languages are added dynamically. It seems the MessageBag object stores only the messages and the corresponding fields but not the rule name.

My solution

I renamed all the validation messages to their corresponding rule name, removed the :attribute parts.

The question

Is there a way (ideally directly in the view) to get the failed validation rule name? For example, instead of using:



I would like to use



I hope I am clear about this. Another scenario I have is an API which, of course, validates user input. I would like to return an array with the broken rules instead of the english messages what will be difficult for a multi-language FrontEnd to accommodate.




via Alex

Advertisement