简体   繁体   English

如何在laravel 5中提供自定义验证消息

[英]how to give custom validation messages in laravel 5

I'v been following on laravel 4.2 and to day tried laravel 5. 我一直在关注laravel 4.2,今天尝试过laravel 5。

how can you add custom validation messages to rules in laravel 5. 如何在laravel 5中将自定义验证消息添加到规则

Code Sample 代码样例

namespace App\Http\Requests;

 
use App\Http\Requests\Request;
 

class MyRequest extends Request {


    /**

     * Determine if the user is authorized to make this request.

     *

     * @return bool

     */

    public function authorize()

    {

        return true;

    }

 

    /**

     * Get the validation rules that apply to the request.

     *

     * @return array

     */

    public function rules()

    {

        return [

            'email' => 'required|email',

            'password' => 'required|min:8'

        ];

    }

}
// in your controller action
public function postLogin(\App\Http\Requests\MyRequest $request)

{

    // code here will not fire

    // if the validation rules

    // in the request fail

}

?>

it's not clear as in 4.2 不清楚像4.2

You can return them from the messages() method like you do with rules¨: 您可以像使用规则¨一样从messages()方法返回它们:

public function messages(){
    return [
        'required' => 'The :attribute field is required.'
    ];
}

As well as the messages() method you can also do this in the lang file ( resources/lang/[language]/validation.php ) if all you want to do is change, for example the message for a unique rule on an email field across the entire app you can do: 除了messages()方法,如果您要更改的内容也可以在lang文件( resources/lang/[language]/validation.php )中执行,例如,电子邮件中唯一规则的消息您可以在整个应用中执行以下操作:

/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
    'email' => [
        'unique' => 'This email is already registered...etc',
    ]
],

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

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