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

lk4*_*404 1 php laravel laravel-5

我一直在关注laravel 4.2,今天尝试了laravel 5。

如何将自定义验证消息添加到 Laravel 5 中的规则?

代码示例

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

}

?>
Run Code Online (Sandbox Code Playgroud)

4.2 中不清楚

Joe*_*Joe 5

除了messages()方法之外,resources/lang/[language]/validation.php如果您只想更改,您还可以在 lang 文件 ( ) 中执行此操作,例如您可以执行的整个应用程序中电子邮件字段上唯一规则的消息:

/*
|--------------------------------------------------------------------------
| 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',
    ]
],
Run Code Online (Sandbox Code Playgroud)