Laravel custom messages to check array

I have a form and I have an array of input fields for videos, now when I check the form, if I have several invalid fields with videos, I get the same message for each invalid field, since I made my own messages. I don’t want the same error message for each input field, and I don’t want the Laravel default error messages for arrays where the name of the field with the error message is indicated, instead I would like to have error messages with the value, in this case, the url written by the user. How to do it?

This is my request file with messages and rules:

public function messages(){ $messages = [ 'title.required' => 'Du mÄ ha tittel.', 'type.required' => 'Du mÄ velge artikkeltype.', 'category.required' => 'Du mÄ velge kategori.', 'summary.required' => 'Du mÄ ha inngress.', 'text.required' => 'Du mÄ ha artikkeltekst.', 'active_url' => 'Du mÄ ha gyldig url.', ]; } public function rules(){ $rules = [ 'external_media.*' => 'active_url', 'title' => 'required', 'type' => 'required', 'category' => 'required', 'summary' => 'required', 'text' => 'required', //'image' => 'required|image|max:20000', ]; return $rules; } 

Updated code to make question clear

When I have my request file, for example:

 public function messages(){ $messages = [ 'title.required' => 'Du mÄ ha tittel.', 'type.required' => 'Du mÄ velge artikkeltype.', 'category.required' => 'Du mÄ velge kategori.', 'summary.required' => 'Du mÄ ha inngress.', 'text.required' => 'Du mÄ ha artikkeltekst.', 'external_media.active_url' => 'Du mÄ ha gyldig url.', ]; return $messages; } public function rules(){ $rules = [ 'external_media.*' => 'active_url', 'title' => 'required', 'type' => 'required', 'category' => 'required', 'summary' => 'required', 'text' => 'required', //'image' => 'required|image|max:20000', ]; return $rules; } 

I get the output:

 The external_media.0 is not a valid URL. The external_media.1 is not a valid URL. The external_media.2 is not a valid URL. 

Instead of such a conclusion, I would like to take a value for each of these inputs and have something like:

 The htt:/asdfas.com is not a valid URL. 
+6
source share
9 answers

To use custom messages outside the verification language file, you can use it as follows:

 $messages = ['username.required' => 'customeError']; $validator = \Validator::make( $data, ['username' => 'required'], messages ); 

You can simply pass an array of your custom messages as the third parameter, as I used it above. Hope this helps.

+4
source
 public function messages() { $messages = [ 'title.required' => 'Du mÄ ha tittel.', 'type.required' => 'Du mÄ velge artikkeltype.', 'category.required' => 'Du mÄ velge kategori.', 'summary.required' => 'Du mÄ ha inngress.', 'text.required' => 'Du mÄ ha artikkeltekst.', ]; foreach ($this->get('external_media') as $key => $val) { $messages["external_media.$key.active_url"] = "$val is not a valid active url"; } return $messages; } 
+2
source
 public function messages() { $messages = []; foreach ($this->request->get('external_media') as $key => $val) { $messages['external_media.' . $key . '.active_url'] = 'The '.$val .' is not a valid URL.' } return $messages; } 
+2
source

I think this will help you if you use the "name = location []" this on your browse page.

  for ($i = 0; $i <= sizeof($location); $i++) { $this->validate($request, [ // set the rules 'location.'.$i => 'required', 'contact_no.'.$i => 'required', 'email.'.$i => 'required|email', ], [ // set your custom error messages here 'location.'.$i.'.'.'required' => 'Contact no. is required', 'contact_no.'.$i.'.'.'required' => 'Contact no. is required', 'email.'.$i.'.'.'required' => 'Email is required', 'email.'.$i.'.'.'email' => 'Please enter valid email address' ]); } 
+1
source

Edit with a Potential Solution

After some digging, I looked at the Validator class and how it adds its error messages to see if it has any placeholders available.

In Illuminate\Validation\Validator function that, it seems to me, runs to validate the request, validate , which runs each of the rules in turn and adds error messages. The piece of code associated with adding the error message is the one at the end of the function:

  $value = $this->getValue($attribute); $validatable = $this->isValidatable($rule, $attribute, $value); $method = "validate{$rule}"; if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) { $this->addFailure($attribute, $rule, $parameters); } 

As you can see, it does not actually transmit the value of the field that was checked when it adds a failure, which in turn adds an error message.

I managed to achieve something to achieve what you need. If you add these two methods to your base Request class, which is usually located in App\Http\Requests\Request :

 protected function formatErrors(Validator $validator) { $errors = parent::formatErrors($validator); foreach ($errors as $attribute => $eachError) { foreach ($eachError as $key => $error) { if (strpos($error, ':value') !== false) { $errors[$attribute][$key] = str_replace(':value', $this->getValueByAttribute($attribute), $error); } } } return $errors; } protected function getValueByAttribute($attribute) { if (($dotPosition = strpos($attribute, '.')) !== false) { $name = substr($attribute, 0, $dotPosition); $index = substr($attribute, $dotPosition + 1); return $this->get($name)[$index]; } return $this->get($attribute); } 

Then, in your verification messages, you can put a substitute :value , which should be replaced with the actual value that was checked, for example:

 public function messages() { return [ 'external_media.*.active_url' => 'The URL :value is not valid' ]; } 

I noticed a couple of problems with your code:

  • In your messages function, you provided a message for active_url , which is the name of the rule, not the name of the field. This must be external_media .
  • You are not returning the $messages variable from your messages function. You need to add return $messages; In the end.

Regarding your question, however, the class in which you write this code is an instance of Illuminate\Http\Request , and you should have access to the actual values ​​provided to this request when generating error messages. For example, you can do something like this:

 public function messages() { return [ 'external_media' => 'The following URL is invalid: ' . $this->get('external_media') ]; } public function rules() { return [ 'external_media' => 'active_url' ]; } 

Which will include the value indicated in external_media in the error message. Hope this helps.

0
source

You can use Error Format Setting

 protected function formatErrors(Validator $validator) { $results = []; $flag = false; $messages = $validator->errors()->messages(); foreach ($messages as $key => $value) { if (!str_contains($key, 'external_media') || !$flag) { $results[] = $value[0]; } if (str_contains($key, 'external_media') && !$flag) { $flag = true; } } return $results; } 
0
source

This works well for me in Laravel 5.5

 $request->validate([ 'files.*' => 'required|mimes:jpeg,png,jpg,gif,pdf,doc|max:10000' ], [ 'files.*.mimes' => 'Los archivos solo pueden estar con formato: jpeg, png, jpg, gif, pdf, doc.', ] ); 
0
source
 'external_media.*.required' => 'active_url', 
0
source

You can use as

 $messages = [ 'title.required' => 'Du mÄ ha tittel.', 'type.required' => 'Du mÄ velge artikkeltype.', 'category.required' => 'Du mÄ velge kategori.', 'summary.required' => 'Du mÄ ha inngress.', 'text.required' => 'Du mÄ ha artikkeltekst.', 'active_url' => 'Du mÄ ha gyldig url.', ]; $validator = Validator::make($data, [ 'external_media.*' => 'active_url', 'title' => 'required', 'type' => 'required', 'category' => 'required', 'summary' => 'required', 'text' => 'required', //'image' => 'required|image|max:20000' ], $messages); if ($validator->fails()){ // handle validation error } else { // no validation error found } 
-1
source

All Articles