Model admin.php
Model (), tabname () is a fixed format
<?php/* Administrator Model *--------------------------------------------* @auther Haoxiang *--------------------------------- -----------* @time 2015/8/3 */class Admin extends Cactiverecord{public $password 1;public $password 2; public static function model ($className = __class__) {return Parent::model ($className); } public Function TableName () {return ' {{admin}} '; }/* Tag name */Public Function attributelabels () {return array (' Password ' = ' original password ', ' password1 ' = ' new password ', ' Password2 ' = ' Confirm password ');} /* * Validation rules * Required fields will have a small asterisk indicating that you must */public function rules () {return array (' password ', ' Required ', ' message ' = = ') The original password is required '), Array (' password1 ', ' Required ', ' message ' = ' new password required '), Array (' password2 ', ' Required ', ' message ' = = ') Confirm password required '), Array (' password2 ', ' compare ', ' compareattribute ' = ' password1 ', ' message ' = ' two times password inconsistent '), Array (' Password ', ' Check_password '),);} /* * Custom validation rules */Public Function Check_password () {$user = YIi::app ()->user->name; $userinfo = Admin::model ()->find (' username =: Name ', Array (': name ' = = $user)); if (MD5 ($this->password)! = $userinfo [' password ']) {$this->adderror (' Password ', ' Username or password error '); } }}
Use in templates
<?php $form = $this->beginwidget (' Cactiveform ', Array (' Enableclientvalidation ' =>true, ' clientoptions ' = = Array (' Validateonsubmit ' =>true));? ><table class= "table" ><tr><td class= "th" colspan= "ten" > Change Password </td></tr><tr><td> user </td><td><?php echo Yii::app ()->user->name? ></td></tr><tr><td><?php echo $form->labelex ($Admin, ' password ')?></td> <td><?php echo $form->passwordfield ($Admin, ' password ')? ><?php echo $form->error ($Admin, ' Password ')? ></td></tr><tr><td><?php echo $form->labelex ($Admin, ' Password1 ')? ></td><td><?php echo $form->passwordfield ($Admin, ' Password1 ')? ><?php Echo $form Error ($Admin, ' Password1 ')? ></td></tr><tr><td><?php echo $form->labelex ($Admin, ' Password2 ')? ></td><td><?php echo $form->passwordfield ($Admin, ' Password2')? ><?php echo $form->error ($Admin, ' Password2 ')? ></TD></TR><TR><TD colspan= "10" ><input type= "Submit" class= "Input_button" value= "Modify"/></td></tr></table><?php $ This->endwidget ()?>
Controller
<?php/* Background User Module */class Usercontroller extends controller{public function actionpasswd () { $Admin = new Admin ( ); Load Model if (isset ($_post[' admin ')) { $Admin->attributes = $_post[' admin ']; if ($Admin->validate ()) { //$Admin->adderror (' Password ', ' unknown error '); $userinfo = $Admin->model ()->find (' username =: Name ', Array (': Name ' =>yii::app ()->user->name)); $password = MD5 ($_post[' Admin ' [' password1 ']); $result = $Admin->model ()->updatebypk ($userinfo->id,array (' password ' = $password)); if ($result) { Yii::app ()->user->setflash (' Success ', ' modified successfully '); }}} $this->render (' passwd ', Array (' Admin ' = = $Admin));} }
Turn on foreground asynchronous validation
<?php $form = $this->beginwidget (' Cactiveform ', Array (' Enableclientvalidation ' =>true, ' clientoptions ' = = Array (' Validateonsubmit ' =>true));?>
List of rules
Boolean: Ensure that the attribute has a Boolean value.
CAPTCHA: Ensure that the attribute value is equal to the verification code shown in Captcha.
Compare: Ensure that the attribute equals another attribute or constant.
Email: Ensure that the feature is a valid email address.
Default: Specifies the default value for the attribute.
exist: Ensure that the attribute values can be found in the columns of the specified table.
file: Make sure that the feature contains the name of an uploaded file.
Filter: This feature is changed by a filter.
Combinatorial: Ensure that the data is within the range of a pre-specified value.
lengths ensure that the length of the data is within a specified range.
match: Make sure the data matches a regular expression.
numerical: Make sure the data is a valid number.
Required: Ensure that the attribute is not empty.
type: Ensure that the attribute is the specified data type.
Unique: Ensure that the data is unique in the column of the data table.
URL: Make sure the data is a valid URL.
Model tags and validation rules, front and back validation