我正在我的 Laravel 应用程序中处理密码更改表单。我想将验证器与自定义错误消息一起使用。
我的代码如下所示:
$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()));
在日志中 自定义消息是显示我的自定义消息,但在下一行中有原始 留言 .
我在 official doc 工作.
有人遇到过这个问题吗?
谢谢提前回答!
最佳答案
重写和推荐的方法。
引用手册https://laravel.com/docs/5.5/validation#creating-form-requests
使用请求文件。
php artisan make:request UpdateUserPasswordRequest
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', ]; }
<?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 } }
关于Laravel 5.5 使用自定义消息进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432025/