在modle层里面的rules里面对应的规则列表
<?php
class User extends CActiveRecord{
public $password1;
public $password2;
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return "{{admin}}";
}
public function attributeLabels(){
return array(
‘password‘ => ‘原始密码‘,
‘password1‘ => ‘新密码‘,
‘password2‘ => ‘确认密码‘
);
}
public function rules(){
return array(
array(‘password‘, ‘required‘, ‘message‘=>‘原始密码必填‘),
array(‘password‘, ‘check_passwd‘),
array(‘password1‘, ‘required‘, ‘message‘=>‘新密码必填‘),
array(‘password2‘, ‘required‘, ‘message‘=>‘确认密码必填‘),
array(‘password2‘, ‘compare‘, ‘compareAttribute‘=>‘password1‘, ‘message‘=>‘两次密码不相同‘),
);
}
public function check_passwd(){
$userInfo = $this->find(‘username=:name‘, array(‘:name‘=>Yii::app()->user->name));
if(md5($this->password) != $userInfo->password){
$this->addError(‘password‘, ‘原始密码不正确‘);
}
}
}
②前台自动验证的方法。因为一般验证,是先通过post方法传到后台,然后后台控制器的方法去验证,这样比较慢。所以可以在view层直接加上这么段小物件的代码,就能实现前台验证了。必须使用render来渲染这个页面,不然js无法加载出来了。
<?php $form=$this->beginWidget(‘CActiveForm‘, array(
‘enableClientValidation‘=>true,//开启前台客户端验证
‘clientOptions‘=>array(//客户端验证
‘validateOnSubmit‘=>true, //提交的时候进行验证
),
)); ?>
时间: 2024-10-09 18:24:31