Laravel 5.5 使用自定义消息进行验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49432025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 validate with custom messages
提问by LakiGeri
I'm working on a pw change form in my laravel app. I want to use the validator with custom error messages.
我正在我的 Laravel 应用程序中处理密码更改表单。我想将验证器与自定义错误消息一起使用。
My code looks like this:
我的代码如下所示:
$rules = [
'username' => 'required|max:255',
'oldpassword' => 'required|max:255',
'newpassword' => 'required|min:6|max:255|alpha_num',
'newpasswordagain' => 'required|same:newpassword',
];
$messages = [
'username.required' => Lang::get('userpasschange.usernamerequired'),
'username.max:255' => Lang::get('userpasschange.usernamemax255'),
'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
'oldpassword.max:255' => Lang::get('userpasschange.oldpasswordmax255'),
'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
'newpassword.min:6' => Lang::get('userpasschange.newpasswordmin6'),
'newpassword.max:255' => Lang::get('userpasschange.newpasswordmax255'),
'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
];
$validator = Validator::make($request->all(), $rules, $messages);
$validator->setCustomMessages($messages);
Log::debug("custommessages: " . json_encode($messages));
Log::debug("messages: " . json_encode($validator->messages()));
In the log custommessagesis shows my custom msgs, but in the next line there is the original messages.
在日志中custommessages显示了我的自定义消息,但在下一行中有原始消息。
I'm working from the official doc.
我正在使用官方文档。
Have anybody meet this problem?
有没有人遇到过这个问题?
Thx for the answers in advance!
谢谢提前回答!
回答by Indra
A rewrite and the recommended way of doing it. Manual for reference https://laravel.com/docs/5.5/validation#creating-form-requests
重写和推荐的方法。参考手册https://laravel.com/docs/5.5/validation#creating-form-requests
Use requests files.
使用请求文件。
- run
php artisan make:request UpdateUserPasswordRequest
- Write the request file
- 跑
php artisan make:request UpdateUserPasswordRequest
- 写入请求文件
Edit feb 2020: in the latest version of Laravel in the authorize method the global auth() object can be used instead of \Auth so \Auth::check() will become auth()->check(). Both still work and will update if something is removed from the framework
2020 年 2 月编辑:在最新版本的 Laravel 中的授权方法中,可以使用全局 auth() 对象代替 \Auth,因此 \Auth::check() 将变为 auth()->check()。如果从框架中删除某些内容,两者仍然有效并且会更新
namespace App\Http\Requests; class UpdateUserPasswordRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { // only allow updates if the user is logged in return \Auth::check(); // edit you can now replace this with return auth()->check(); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'username' => 'required|max:255', 'oldpassword' => 'required|max:255', 'newpassword' => 'required|min:6|max:255|alpha_num', 'newpasswordagain' => 'required|same:newpassword', ]; } /** * Get the validation attributes that apply to the request. * * @return array */ public function attributes() { return [ 'username' => trans('userpasschange.username'), 'oldpassword' => trans('userpasschange.oldpassword'), 'newpassword' => trans('userpasschange.newpassword'), 'newpasswordagain' => trans('userpasschange.newpasswordagain'), ]; } /** * Get the validation messages that apply to the request. * * @return array */ public function messages() { // use trans instead on Lang return [ 'username.required' => Lang::get('userpasschange.usernamerequired'), 'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'), 'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'), 'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'), 'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'), 'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'), 'newpassword.alpha_num' =>Lang::get('userpasschange.newpasswordalpha_num'), 'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'), 'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'), 'username.max' => 'The :attribute field must have under 255 chars', ]; }
namespace App\Http\Requests; class UpdateUserPasswordRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { // only allow updates if the user is logged in return \Auth::check(); // edit you can now replace this with return auth()->check(); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'username' => 'required|max:255', 'oldpassword' => 'required|max:255', 'newpassword' => 'required|min:6|max:255|alpha_num', 'newpasswordagain' => 'required|same:newpassword', ]; } /** * Get the validation attributes that apply to the request. * * @return array */ public function attributes() { return [ 'username' => trans('userpasschange.username'), 'oldpassword' => trans('userpasschange.oldpassword'), 'newpassword' => trans('userpasschange.newpassword'), 'newpasswordagain' => trans('userpasschange.newpasswordagain'), ]; } /** * Get the validation messages that apply to the request. * * @return array */ public function messages() { // use trans instead on Lang return [ 'username.required' => Lang::get('userpasschange.usernamerequired'), 'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'), 'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'), 'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'), 'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'), 'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'), 'newpassword.alpha_num' =>Lang::get('userpasschange.newpasswordalpha_num'), 'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'), 'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'), 'username.max' => 'The :attribute field must have under 255 chars', ]; }
- In UserController
- 在用户控制器中
<?php namespace App\Http\Controllers; // VALIDATION: change the requests to match your own file names if you need form validation use App\Http\Requests\UpdateUserPasswordRequest as ChangePassRequest; //etc class UserCrudController extends Controller { public function chnagePassword(ChangePassRequest $request) { // save new pass since it passed validation if we got here } }
<?php namespace App\Http\Controllers; // VALIDATION: change the requests to match your own file names if you need form validation use App\Http\Requests\UpdateUserPasswordRequest as ChangePassRequest; //etc class UserCrudController extends Controller { public function chnagePassword(ChangePassRequest $request) { // save new pass since it passed validation if we got here } }
回答by Taras Lonevsky
After you've indicated the messages in Validator::make
在您指出消息后 Validator::make
$validator = Validator::make($request->all(), $rules, $messages);
you shouldn't indicate them again
你不应该再次指出他们
$validator->setCustomMessages($messages); // don't do that
NOTE
笔记
The betterway to use Request validation is to move them to another file
回答by Стефан Рибак
$messages = [
'username.required' => Lang::get('userpasschange.usernamerequired'),
'username.max' => Lang::get('userpasschange.usernamemax255'),
'oldpassword.required' => Lang::get('userpasschange.oldpasswordrequired'),
'oldpassword.max' => Lang::get('userpasschange.oldpasswordmax255'),
'newpassword.required' => Lang::get('userpasschange.newpasswordrequired'),
'newpassword.min' => Lang::get('userpasschange.newpasswordmin6'),
'newpassword.max' => Lang::get('userpasschange.newpasswordmax255'),
'newpassword.alpha_num' => Lang::get('userpasschange.newpasswordalpha_num'),
'newpasswordagain.required' => Lang::get('userpasschange.newpasswordagainrequired'),
'newpasswordagain.same:newpassword' => Lang::get('userpasschange.newpasswordagainsamenewpassword'),
];
Try to dont use this :255 and :6 endings.
尽量不要使用这个 :255 和 :6 结尾。
Wrong:
错误的:
'username.max:255' => Lang::get('userpasschange.usernamemax255'),
Correct:
正确的:
'username.max' => Lang::get('userpasschange.usernamemax255'),