laravel实现密码修改和自定义验证规则及消息
路由:
创建一个Request来验证表单
1 php artisan make:request AdminPost
AdminPost代码:
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Auth; use Validator; use Hash; class AdminPost extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return Auth::guard('admin')->check(); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $this->newValidator(); return [ 'oldPassword'=>'sometimes|required|check_pwd', 'password'=>'sometimes|required|confirmed', 'password_confirmation'=>'sometimes|required', ]; } public function messages() { return [ 'password.required'=>'密码不能为空', 'oldPassword.required'=>'密码不能为空', 'password_confirmation.required'=>'密码不能为空', 'password.confirmed'=>'两次密码不一致', 'oldPassword.check_pwd'=>'原始密码输入有误', ]; } public function newValidator(){ Validator::extend('check_pwd', function($attribute, $value, $parameters, $validator){ return Hash::check($value, Auth::guard('admin')->user()->password); }); } }
控制器处理代码
namespace App\Http\Controllers\Admin; use App\Http\Requests\AdminPost; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Model\Admin; use Auth;
public function edit(){ return view('admin.login.editinfo'); } public function editAction(AdminPost $request){ $model=Auth::guard('admin')->user(); $model->password=bcrypt($request['password']); $model->save(); if ($model->save()){ Auth::guard('admin')->logout(); return redirect('admin/index'); } }
其中有几个关键点:
1.输入新密码和重复密码怎么样确认是否一样
首先name名一定要取对一定要用下面格式
验证规则这么写
2.怎么样确认输入的原始密码是否正确
在AdminPost.php中添加代码