4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
新闻详情
Yii密码验证的实现过程-码农场
来自 : www.hankcs.com/appos/yii-mi-ma 发布时间:2021-03-24

最近在看《应用Yii和PHP进行敏捷Web开发》的过程中,发现了一处有意思的地方。作者为了验证用户两次输入的密码是否一致,在class User中声明一个public $password_repeat;,然后在public function rules()中加了两句:

array(\'password\', \'compare\'),array(\'password_repeat\', \'safe\'),

忽略表单的修改,只用这么几句就实现了password的验证,实在是令人惊奇:

作者解释道:幸运地是,Yii 的另外一个内部验证器 (CCompareValidator),可以完成你所期望的工作。他的工作是对比 2 个属性值,并且在不相等时返回一个错误消息。

看到这里我十分疑惑,没有一句代码告诉CCompareValidator,User类里面有一个$password_repeat是用来比较的,Yii框架是怎么知道有这个变量存在的呢?

后来我去查了查api文档http://www.yiiframework.com/doc/api/1.1/CCompareValidator#compareAttribute-detail 。关于CCompareValidator有这么一段解释:

The value being compared with can be another attribute value (specified via compareAttribute) or a constant (specified via compareValue. When both are specified, the latter takes precedence. If neither is specified, the attribute will be compared with another attribute whose name is by appending _repeat to the source attribute name.

当没有指定变量的时候,Yii将会默认查找XXX_repeat作为比较的对象。看来Yii里面都是这种约定俗成的小tricks呀

\"知识共享许可协议\"知识共享署名-非商业性使用-相同方式共享:码农场 Yii密码验证的实现过程

继续浏览有关 PHP 的文章

本文链接: http://yiima.immuno-online.com/view-715772.html

发布于 : 2021-03-24 阅读(0)
公司介绍
品牌分类
催化剂和助剂 Others
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616