在使用Yii时遇到这样的需求:在一个注册的页面输入两次密码,并验证两次输入是否一致。可是password的repeat的字段在数据库 并不存在。问题来了,如何创建一个password_repeat的属性,进行密码验证。最偷懒的方法就是利用yii自带的验证器。在这里记录下实现的方 法。

假设项目结构如下

protected/models/User.php

proteced/controllers/SiteController.php

protected/views/site/forgot.php

首先在User.php添加一个public的password_repeat属性,该属性会自动映射到rules中。注意on属性,是决定rule应用的场景,默认的场景是insert。在这里,我应用在forgot场景。

class User extends CActiveRecord
{
public $password_repeat;
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
...
array('password_repeat', 'required' , 'on' => 'forgot'),
array('password', 'compare', 'compareAttribute'=>'password_repeat' ,'on'=>'forgot'),
 
);
}
}

在SiteController.php中的actionForgot方法中,添加一个User的model。

public function actionForgotPassword(){
$model = new User();
//set current scenario
$model->scenario = 'forgot';
$User = Yii::app()->getRequest()->getParam('User');
if($User){
$model->attributes = $User;
Helper::performAjaxValidation($model);
....
}
$this->render('forgot',array( 'model' => $model));
}
//Helper的ajax 验证方法,这个在默认生成的controller可以找到
static function performAjaxValidation($model)
{
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}

在视图文件中forgot.php,添加password_repeat字段。

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
)); ?>
<ul>
<li>
<?php echo $form->label($model,'password' , array( 'label' => 'Your New Password')); ?>
<?php echo $form->passwordField($model,'password',array('size'=>32,'maxlength'=>32)); ?>
<?php echo $form->error($model,'password'); ?>
</li>
<li>
<?php echo $form->labelEx($model,'password_repeat' , array( 'label' => 'Repeat Password')); ?>
<?php echo $form->passwordField($model,'password_repeat',array('size'=>32,'maxlength'=>32)); ?>
<?php echo $form->error($model,'password_repeat'); ?>
</li>
</ul>

这样就实现了password的验证了。

Yii实现Password Repeat Validate Rule的更多相关文章

  1. Yii源码阅读笔记(十五)

    Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...

  2. Validate on POST data

    1. Basic validate  on bean's attribute. @Notnull @Max @Min @Pattern ... 2. Validate by logic 1) pass ...

  3. jquery.validate.js表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  4. jQuery.Validate验证库详解

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  5. jQuery.Validate验证库

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  6. jquery validate.js表单验证的基本用法入门

    这里转载一篇前辈写的文章,在我自己的理解上修改了一下,仅作记录. 先贴一个国内某大公司的代码: 复制代码 代码如下: <script type="text/javascript&quo ...

  7. jquery.validate使用详解

    一.简单应用实例: 1.用class样式进行验证,用法简单,但不能自定义错误信息,只能修改jquery-1.4.1.min.js中的内置消息,也不支持高级验证规则. <script type=& ...

  8. 【转】jquery.validate.js表单验证

    一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...

  9. Laravel validate 500异常 添加手机验证,中文验证与Validator验证的“半个”生命周期

    今天来讲一下,Lumen的Validator函数 1 2 3 4 5 6 7 8 9 10 11 use Validator;   ...   Class .. {   public function ...

随机推荐

  1. android 5.1 API简介

    android 5.1介绍: http://developer.android.com/about/versions/android-5.1.html?utm_campaign=lollipop-51 ...

  2. json中文乱码问题

    首先在 tomcat的 D:\apache-tomcat-7.0.57\conf\server.xml里添加中文编码 <Connector port="8080" proto ...

  3. AIM Tech Round 3 (Div. 2) B 数学+贪心

    http://codeforces.com/contest/709 题目大意:给一个一维的坐标轴,上面有n个点,我们刚开始在位置a,问,从a点开始走,走n-1个点所需要的最小路程. 思路:我们知道,如 ...

  4. PAT (Advanced Level) 1069. The Black Hole of Numbers (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  5. StrictMode使用详解

    http://hb.qq.com/a/20110914/000054.htm http://www.android100.org/html/201204/25/1097.html http://www ...

  6. SVN MERGE 和冲突

    摘要:最佳做法是避免冲突.冲突时,不要把branch merge到trunk. 先由最新版本的trunk得到branch,然后再修改文件,直接merge过去就行.这样不会有冲突.先用svn merge ...

  7. ASP.NET MVC3中的路由系统(Routes) .

    MVC中,用户访问的地址并不映射到服务器中对应的文件,而是映射到对应Control里对应的ActionMethod,由ActionMethod来决定返回用户什么样的信息.而把用户访问的地址对应到对应的 ...

  8. Counting Haybales

    Counting Haybales 题目描述 Farmer John is trying to hire contractors to help rearrange his farm, but so ...

  9. c语言基本数据类型short、int、long、char、float、double

    C 语言包含的数据类型如下图所示: 一.数据类型与“模子”short.int.long.char.float.double 这六个关键字代表C 语言里的六种基本数据类型. 怎么去理解它们呢? 举个例子 ...

  10. 用free -m查看的结果:

    用free -m查看的结果: # free -m          total    used    free     shared buffers     cached Mem:           ...