I am having a form and I have an array of input fields for video urls, now when I validate form if I have multiple invalid fields with video urls, I get the same message for each of the invalid field, since I made my own custom messages. I don't want for each input field the same error message and I don't want the default Laravel error messages for arrays where the name of the field is shown with the error message, instead of that, I would like to have error messages with the value, in this case url written from the user. How to do that?
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 the question clearer
When I have my request file like this:
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 that kind of output I would like to take the value for each of those inputs and have something like:
The htt:/asdfas.com is not a valid URL.
Custom Error Messages In many cases, you may wish to specify your attribute specific custom messages in a language file instead of passing them directly to the Validator . To do so, add your messages to custom array in the resources/lang/xx/validation. php language file.
You can make the code powerful by using the Laravel custom validation rule with parameters. This streamlines the flow between the logic and code wherever required. Laravel offers various convenient validation rules that can be applied to the data if values are specific to a database table.
To get the exact words to validate you can make use of Rule::in method available with laravel. Using Rule::in method whatever the values provided by this rule has to be matched otherwise it will fail.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With