用于数组验证的Laravel自定义消息

Mar*_*rco 7 php laravel

我有一个表单,我有一个视频网址的输入字段数组,现在当我验证表单,如果我有多个无效字段与视频网址,我得到相同的消息为每个无效字段,因为我做了我自己的自定义消息.我不希望每个输入字段都有相同的错误消息,我不希望数组的默认Laravel错误消息,其中字段的名称显示错误消息,而不是,我想有错误消息使用值,在这种情况下从用户写入url.怎么做?

这是我的请求文件,包含消息和规则:

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;

  }
Run Code Online (Sandbox Code Playgroud)

更新了代码以使问题更加清晰

当我有我的请求文件时:

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;

  }
Run Code Online (Sandbox Code Playgroud)

我得到输出:

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.
Run Code Online (Sandbox Code Playgroud)

而不是那种输出我想取每个输入的值,并有类似的东西:

The htt:/asdfas.com  is not a valid URL.
Run Code Online (Sandbox Code Playgroud)

Dan*_*tum 7

对于 laravel 7.x,我找到了以下解决方案。基本上你可以使用'field.rule' => 'message'

public function rules() 
{
   return [
      'user.*.firstname' => 'string|required',
   ];
}
Run Code Online (Sandbox Code Playgroud)

然后在消息中(我对所有请求使用 FormRequest):

public function messages() 
{
   'user.*.firstname.required' => 'Firstname of the user is required',
}
Run Code Online (Sandbox Code Playgroud)

您还可以将翻译字符串传递'passwords.user'给消息。


Osa*_*yed 6

要使用来自验证语言文件外部的自定义消息,您可以这样使用它:

$messages = ['username.required' => 'customeError'];

$validator = \Validator::make(
    $data,
    ['username' => 'required'],
    messages
);
Run Code Online (Sandbox Code Playgroud)

您可以将自定义消息数组作为第三个参数传递,就像我上面使用的那样。希望这可以帮助。


smo*_*o0f 6

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;

}
Run Code Online (Sandbox Code Playgroud)


小智 -3

您可以用作

\n\n
$messages = [\n    'title.required' => 'Du m\xc3\xa5 ha tittel.',\n    'type.required' => 'Du m\xc3\xa5 velge artikkeltype.',\n    'category.required' => 'Du m\xc3\xa5 velge kategori.',\n    'summary.required' => 'Du m\xc3\xa5 ha inngress.',\n    'text.required' => 'Du m\xc3\xa5 ha artikkeltekst.',\n    'active_url' => 'Du m\xc3\xa5 ha gyldig url.',\n];\n\n$validator = Validator::make($data, [\n    'external_media.*' => 'active_url',\n    'title' => 'required',\n    'type' => 'required',\n    'category' => 'required',\n    'summary' => 'required',\n    'text' => 'required',\n    //'image' => 'required|image|max:20000'\n], $messages);\n\nif ($validator->fails()){\n    // handle validation error\n} else {\n    // no validation error found\n}\n
Run Code Online (Sandbox Code Playgroud)\n