Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel custom messages for array validation

Tags:

php

laravel

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.
like image 240
Ludwig Avatar asked Jun 20 '16 14:06

Ludwig


People also ask

What is the method used for specifying custom messages for validator errors in form request?

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.

What is custom validation in laravel?

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.

How do you validate exact words in laravel?

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.


1 Answers

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;

}
like image 192
smo0f Avatar answered Oct 13 '22 00:10

smo0f