doujiu8826
2015-10-02 04:31
浏览 31
已采纳

如何在Laravel中设置自定义验证消息?

I want to show my own message instead of one provided by the framework, So I did the following but it is not making any difference and showing default messages.

$message = [];
    foreach($request->get('estimates') as $key => $val)
    {
        $rules['estimates.'.$key] = 'required|integer';
        $message['estimates.'.$key] = 'Incorrect value';
    }
    $rules['technical_information'] = 'required';
    $rules['project_risks'] = 'required';

    $this->validate($request, $rules,$message);

图片转代码服务由CSDN问答提供 功能建议

我想显示自己的消息,而不是框架提供的消息,所以我做了以下但不是 产生任何差异并显示默认消息。

  $ message = []; 
 foreach($ request-> get('estimate')as $ key => $  val)
 {
 $ rules ['估计。'。$ key] ='required | integer'; 
 $ message ['估计。'。$ key] ='不正确的值'; 
} 
 $  rules ['technical_information'] ='required'; 
 $ rules ['project_risks'] ='required'; 
 
 $ this-> validate($ request,$ rules,$ message); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duananyu9231 2015-10-02 04:57
    已采纳

    I believe you need to adhere to the correct format for messages. Per the Laravel docs:

    $messages = [
        'required' => 'The :attribute field is required.',
    ];
    $validator = Validator::make($input, $rules, $messages);
    

    The key in $messages needs to be the rule , and the value is the Message -- with a placeholder for the attribute name.

    You can also specify messages per rule, per attribute by using dot syntax:

    'username.required' => 'the field is required...'
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题