doujiu8826 2015-10-01 20:31
浏览 39
已采纳

如何在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);
  • 写回答

1条回答 默认 最新

  • duananyu9231 2015-10-01 20: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...'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?