Lak*_*eri 1 laravel laravel-5 laravel-5.5
我正在使用我的laravel应用程序中的pw更改表单.我想将验证器与自定义错误消息一起使用.
我的代码看起来像这样:
$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()));
Run Code Online (Sandbox Code Playgroud)
在日志中,custommessages显示我的自定义消息,但在下一行中有原始消息.
我正在从官方文件工作.
有人遇到这个问题吗?
Thx提前给出答案!
重写和推荐的方法.参考手册https://laravel.com/docs/5.5/validation#creating-form-requests
使用请求文件.
php artisan make:request UpdateUserPasswordRequest
Run Code Online (Sandbox Code Playgroud)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(); } /** * 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: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'), 'username.max:255' => 'The :attribute field must have under 255 chars', ]; }
Run Code Online (Sandbox Code Playgroud)<?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 7.x、6.x、5.x
定义自定义规则后,您可以在控制器验证中使用它,例如:
$validatedData = $request->validate([
'f_name' => 'required|min:8',
'l_name' => 'required',
],
[
'f_name.required'=> 'Your First Name is Required', // custom message
'f_name.min'=> 'First Name Should be Minimum of 8 Character', // custom message
'l_name.required'=> 'Your Last Name is Required' // custom message
]
);
Run Code Online (Sandbox Code Playgroud)
对于本地化,您可以使用:
['f_name.required'=> trans('user.your first name is required'],
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助...
| 归档时间: |
|
| 查看次数: |
9919 次 |
| 最近记录: |