简体   繁体   English

如何通过扩展Laravel 5中的Validator类来添加自定义错误消息?

[英]How can I add a custom error message by extending the Validator class in Laravel 5?

I want to validate alpha_dash(Alphabets and Spaces) and the code below works fine 我想验证alpha_dash(字母和空格),下面的代码工作正常

Validator::extend('alpha_spaces', function($attribute, $value)
{
    return preg_match("/^[a-z0-9 .\-]+$/i", $value);
});

but the error it gives is not user friendly : 但是它给出的错误不是用户友好的:

validation.alpha_spaces

How can change this message? 如何更改此消息?

This is the method where it is posts 这是张贴的方法

public function create(Request $request)
{

    $this->validate($request, [
        'title' => 'required|alpha_spaces|max:255',
    ]);


}

Thanks! 谢谢!

Just add your custom error message as an array element to resources/lang/xx/validation.php : 只需将您的自定义错误消息作为数组元素添加到resources/lang/xx/validation.php

'alpha_spaces' => 'The :attribute may only contain letters, numbers and spaces.',

Read more: http://laravel.com/docs/5.0/validation#custom-error-messages 了解更多: http : //laravel.com/docs/5.0/validation#custom-error-messages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM