confirmed

验证字段必须有一个匹配字段 foo_confirmation,例如,如果验证字段是 password,必须输入一个与之匹配的 password_confirmation 字段。

same:field

给定字段和验证字段必须匹配

protected $fillable = ['name', 'password'];

public static $rules = [

'name' => 'required|unique:managers',

'password' => 'required|confirmed',

'password_confirmation' => 'required|same:password'

];

public static function error_message()

{

return [

'name.required' => __('tyvalidation.name'),

'name.unique' => __('tyvalidation.unique'),

'password.required' => __('tyvalidation.password'),

'password.confirmed' => __('tyvalidation.confirmed'),

];

}

public function setPasswordAttribute($value)

{

$this->attributes['password'] = Hash::make($value);

}

经验证,上面的验证方式在update的时候会出问题,修改的时候会验证unique,导致不能保存,所以需要修改下。

官网说:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.

To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the |character to delimit the rules:

重要的2句话是:

有时,您可能希望在唯一检查期间忽略给定的ID。

当然,您需要验证电子邮件地址是否唯一。但是,如果用户仅更改名称字段而不更改电子邮件字段,则不希望抛出验证错误,因为用户已经是电子邮件地址的所有者,为了指示验证者忽略用户的ID,我们将使用Rule该类来流畅地定义规则。

use Illuminate\Validation\Rule;

Validator::make($data, [

'email' => [

'required',

Rule::unique('users')->ignore($user->id),

],

]);

所以修改为

'name' => [

'required',

Rule::unique('managers')->ignore($id),

],

在更新密码时,我们需要验证旧的密码是否正确,那我们需要使用自定义验证。

Using Closures

If you only need the functionality of a custom rule once throughout your application, you may use a Closure instead of a rule object. The Closure receives the attribute's name, the attribute's value, and a $fail callback that should be called if validation fails:

Closure接收属性的名称,属性的值以及$fail在验证失败时应调用的回调。

$validator = Validator::make($request->all(), [

'title' => [

'required',

'max:255',

function($attribute, $value, $fail) {

if ($value === 'foo') {

return $fail($attribute.' is invalid.');

}

},

],

]);

所以密码是否正确可以这样验证

'old_password' => [

'required',

function($attribute, $value, $fail) use ($manager)

{

if (!Hash::check($value, $manager->password))

{

return $fail(__('tyvalidation.old_password'));

}

},

],

所有代码如下:

create.html

{!! __('tycms.name') !!}

T

@foreach ($errors->get('name') as $message)

{{ $message }}

@endforeach

{!! __('tycms.password') !!}

T

@foreach ($errors->get('password') as $message)

{{ $message }}

@endforeach

{!! __('tycms.confirm_password') !!}

T

@foreach ($errors->get('password') as $message)

{{ $message }}

@endforeach

store

$input_all = $request->all();

$validator = Validator::make($input_all, Manager::rules(), Manager::error_message());

if ($validator->fails())

{

return redirect()

->action($this->class_basename . '@create')

->withErrors($validator)

->withInput();

}

$model = Manager::create($input_all);

edit.html

{!! __('tycms.name') !!}

T

@foreach ($errors->get('name') as $message)

{{ $message }}

@endforeach

{!! __('tycms.old_password') !!}

T

@foreach ($errors->get('old_password') as $message)

{{ $message }}

@endforeach

{!! __('tycms.password') !!}

T

@foreach ($errors->get('password') as $message)

{{ $message }}

@endforeach

{!! __('tycms.confirm_password') !!}

T

@foreach ($errors->get('password') as $message)

{{ $message }}

@endforeach

update

$input_all = $request->all();

$model = $this->findById($id);

$validator = Validator::make($input_all, Manager::rules($id, $model), Manager::error_message());

if ($validator->fails())

{

return redirect()

->action($this->class_basename . '@edit', ['id' => $id])

->withErrors($validator)

->withInput();

}

$model->fill($input_all);

$model->save();

Models\Manager

protected $table = 'managers';

protected $fillable = ['name', 'password'];

/*public static $rules = [

'name' => 'required|unique:managers',

'password' => 'required|confirmed',

'password_confirmation' => 'required|same:password'

];*/

public static function rules ($id = null, $manager = null)

{

if (empty($id))

{

$rules = [

'name' => 'required|unique:managers',

'password' => 'required|confirmed',

'password_confirmation' => 'required|same:password'

];

} else

{

$rules = [

'name' => [

'required',

Rule::unique('managers')->ignore($id),

],

'old_password' => [

'required',

function($attribute, $value, $fail) use ($manager)

{

if (!Hash::check($value, $manager->password))

{

return $fail(__('tyvalidation.old_password'));

}

},

],

'password' => 'required|confirmed',

'password_confirmation' => 'required|same:password'

];

}

return $rules;

}

public static function error_message()

{

return [

'name.required' => __('tyvalidation.name'),

'name.unique' => __('tyvalidation.unique'),

'password.required' => __('tyvalidation.password'),

'password.confirmed' => __('tyvalidation.confirmed'),

];

}

public function setPasswordAttribute($value)

{

$this->attributes['password'] = Hash::make($value);

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

确认密码参数php,laravel unique验证、确认密码confirmed验证以及密码修改验证的方法...相关推荐

  1. php怎么写确认密码,如何在Laravel 5中验证当前密码,新密码和新密码的确认? - php...

    我已经在UserController@getProfilePassword和UserController@postProfilePassword中创建了密码路由,视图和方法 目前,如果我填写new_p ...

  2. html正则表达式确认密码,如何使用正则表达式在流星中验证确认密码

    我使用meteor开发我的应用程序.我需要验证我的注册表单,以便限制用户.现在,我在我的注册表单中输入密码并确认密码.我正在使用正则表达式进行验证.如何使用正则表达式在流星中验证确认密码 我的代码是: ...

  3. js表单验证确认密码输入一致

    <form action="../01-自动轮播图片/图片自动轮播.html" onsubmit="return checkForm()" >用户名 ...

  4. android确认密码代码,Android手机卫士之确认密码对话框

    本文接着实现"确认密码"功能,也即是用户以前设置过密码,现在只需要输入确认密码 布局文件和<Android 手机卫士--设置密码对话框>中的布局基本类似,所有copy一 ...

  5. 台达s1变频器参数表_变频器被加密,有这些超级密码,不用慌(各种品牌都有,建议收藏...

    西门子品牌 6SE70书本型变频器:设定密码打不开时,将P358和P359中数据改为相同即可. ABB品牌 ACS600变频器:在16.03参数中输入密码"23032",102.0 ...

  6. mysql密码参数_MySQL 密码参数配置与修改 validate_password

    MySQL 密码参数配置与修改 validate_password 场景 通过root用户创建travel_agency数据库,目标是,新建一个用户然后对仅对该用户开放travel_agency数据库 ...

  7. linux用户密码管理,Linux_详解Linux中的用户密码管理命令passwd和change,passwd 修改用户密码参数 nbsp - phpStudy...

    详解Linux中的用户密码管理命令passwd和change passwd 修改用户密码 参数 -k 保持未过期身份验证令牌 -l 关闭账号密码.效果相当于usermod -L,只有root才有权使用 ...

  8. appscan漏洞之查询中的密码参数

    最近用appscan扫描系统发现一个漏洞,是查询中的密码参数,原因是可能会窃取查询字符串中发送的敏感数据,根据扫描建议发送敏感信息时,始终使用 SSL 和 POST(主体)参数. 在排查问题时可能是因 ...

  9. php登陆页面修改密码的功能,使用bootstrap创建登录注册页面并实现表单验证功能...

    本篇文章给大家介绍一下使用bootstrap创建登录注册页面并实现单验证功能的方法.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 用bootstrap做登入注册页面,使用valid ...

最新文章

  1. 雪花算法原理_低照度摄像机原理及影响图像效果因素
  2. Fastreport.Net用户手册:报表对象
  3. linux下达梦数据库启动_linux 平台 达梦DM 7 数据库 启动与关闭
  4. 程序员修神之路--高并发优雅的做限流(有福利)
  5. java中的break与continue用法
  6. 命令行运行jmeter脚本
  7. 微信企业号-根据code获取成员信息(过期code)
  8. 用hundred造句子_2020朋友圈感恩节祝福语大全:微信感恩节鞠躬的图片文案句子说说[多图]...
  9. Excel VBA利用事件对图表自动更新
  10. Axure电商后台业务管理系统原型模板+app电商原型交互+移动端电商通用PRD文档+全局交互用例说明+Axure高保真电商社交prd文档
  11. 【ASM】udev简介及配置、多路径(multipath)等
  12. java shell_jshell – Java Shell
  13. 小话设计模式五:模板方法模式
  14. java开发学生管理系统
  15. tumblr_向您的Tumblr博客添加高级主题
  16. 区块链技术的风险!(转载)
  17. VBS脚本统计红楼梦中贾宝玉出现的次数
  18. ABAQUS应用中的小技巧
  19. Spring JDBC与事务管理
  20. layui的确认弹出层的玩法

热门文章

  1. JeecgBoot 2.x版本SQL漏洞补丁发布——响应零日漏洞修复计划
  2. IDEA 自动生成类注释和方法注释
  3. 【JEECG 官方】技术支持联系方式
  4. 中间件:ElasticSearch组件RestHighLevelClient用法详解
  5. Redis, Memcache 基本使用
  6. 骨传导技术:帮你摆脱噪音的困扰
  7. 408业务课·计算机网络——【考研随笔】之一
  8. [翻译:ASP.NET MVC 教程]理解模型、视图和控制器
  9. [转]Dynamic and static Rectangle in WPF
  10. java获取method,2.5 反射——Class对象功能_获取Method