2
votes

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:

{{ $errors->first('email') }}

I would like to use

{{ $errors->firstRule('email') }}

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.

1
Looking at the API docs for the messageBag class, this doesn't seem to be possible. At least there is no method for accessing that information. laravel.com/api/5.4/Illuminate/Support/MessageBag.htmlJoe

1 Answers

1
votes

If you use FormRequest you could get validator instance inside methods like below:

$this->getValidatorInstance();

Then, if you need to get names of failed rules, just call code below:

$this->getValidatorInstance()->failed();