laravel:确认密码的验证器(10.27.0)
一,相关文档
https://learnku.com/docs/laravel/10.x/validation/14856#rule-confirmed
说明:
confirmed#: 验证字段必须与 {field}_confirmation 字段匹配。例如,如果验证字段是 password,则输入中必须存在相应的 password_confirmation 字段。
二,创建request
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan make:request PasswordRequest
INFO Request [app/Http/Requests/PasswordRequest.php] created successfully.
三,php代码
1,app/Http/Requests/PasswordRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<?php namespace App\Http\Requests; use Illuminate\Contracts\Validation\Validator; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Http\Exceptions\HttpResponseException; class PasswordRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { //return false; return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ 'originpass' => 'required | min:6 | max:20' , 'newpass' => 'required | confirmed | min:6 | max:20' , 'newpass_confirmation' => 'required | min:6 | max:20' , ]; } /** * 获取已定义验证规则的错误消息。 * * @return array */ public function messages() { return [ 'originpass.required' => '原始密码必须输入' , 'originpass.min' => '原始密码最短不少于6个字符' , 'originpass.max' => '原始密码最长不多于二十个字符' , 'newpass.required' => '新密码必须输入' , 'newpass.confirmed' => '新密码与再次输入新密码需要一致' , 'newpass.min' => '新密码最短不少于6个字符' , 'newpass.max' => '新密码最长不多于二十个字符' , 'newpass_confirmation.required' => '新密码必须再次输入' , 'newpass_confirmation.min' => '再次输入新密码最短不少于6个字符' , 'newpass_confirmation.max' => '再次输入新密码最长不多于二十个字符' , ]; } //重写返回 public function failedValidation(Validator $validator ) { throw ( new HttpResponseException(response()->json([ 'code' => 500, 'msg' => $validator ->errors()->first(), ], 200))); } } |
2,controller:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/* 修改密码 */ function password(PasswordRequest $request ){ $originpass = $request ->post( 'originpass' ); $newpass = $request ->post( 'newpass' ); $checkpass = $request ->post( 'newpass_confirmation' ); echo "原始密码:" . $originpass . "<br/>" ; echo "新密码:" . $newpass . "<br/>" ; echo "检查新密码:" . $checkpass . "<br/>" ; return ; } |
说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/11/01/laravel-que-ren-mi-ma-de-yan-zheng-qi-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com
四,测试结果:
五,查看laravel框架的版本:
liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0