我的 laravel 应用程序中有一个管理仪表板从仪表板管理员可以编辑用户配置文件内容。这是我当前的用户控制器(仅包括更新功能)public function update(Request $request, $id){ $this->validate($request, [ 'name' => ['required', 'alpha','min:2', 'max:255'], 'last_name' => ['required', 'alpha','min:2', 'max:255'], 'email' => ['required','email', 'max:255', 'unique:users,email,'.$id], 'password' => ['same:confirm-password','regex:/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{12,}$/'], 'mobile'=>['required', 'regex:/^\+[0-9]?()[0-9](\s|\S)(\d[0-9]{9})$/','numeric','min:9'], 'username'=>['required', 'string', 'min:4', 'max:10', 'unique:users,username,'.$id], 'roles'=>['required'], //'user_roles'=>['required'], ]); $input = $request->all(); if(!empty($input['password'])){ $input['password'] = Hash::make($input['password']); }else{ $input = array_except($input,array('password')); } $user = User::find($id); $user->update($input); DB::table('model_has_roles')->where('model_id',$id)->delete(); $user->assignRole($request->input('roles')); if($input['roles']=='customer'){ return redirect()->route('customers.index') ->with('success','Customer has been updated successfully'); } else{ return redirect()->route('users.index') ->with('success','User has been updated successfully'); }}现在我的问题是,当管理员在不更改密码字段的情况下更新用户时,密码字段得到验证并向我抛出与密码相关的正则表达式错误......如何避免该问题并仅在更改密码字段时验证密码字段。
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消