gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:57:50 26 4
gpt4 key购买 nike

我一直在关注 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

}

?>

和4.2一样不清楚

最佳答案

除了 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',
]
],

关于php - 如何在 Laravel 5 中提供自定义验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28811423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com