This article describes the implementation method of Yii user registration form verification. Share to everyone for your reference, specific as follows:
View Layer: register.php
<?php//using small objects to generate form element $form = $this->beginwidget (' cactiveform ');?> <!--username--> <?php Echo $form-> Labelex ($model, ' username ');? > <?php echo $form->textfield ($model, ' username ');? > <?php echo $form->error ($model, ' username ');? > <br> <!--password--> <?php echo $form->labelex ($model, ' password ');? > <?php echo $form->passwordfield ($model, ' password ');? > <?php echo $form->error ($model, ' password ');? > <br> <!--Confirm password--> <?php echo $form->labelex ($model, ' password2 ');? > <?php echo $form->passwordfield ($model, ' password2 ');? > <?php echo $form->error ($model, ' password2 ');? > <br> <!--mailbox--> <?php echo $form->labelex ($model, ' email '); > <?php echo $form->textfield ($model, ' email ');? > <?php echo $form->error ($model, ' email ');? > <br> <!--gender--> <?php Echo $form->labelex ($model, ' sex ');? > <?php echo $form->radiobuttonlist ($model, ' sex ', Array (1=> ' Male ',2=> ' female '), Array ("separator" => " "));? > <br> <!--grade--> <?php echo $form->labelex ($model, ' grade ');? > <?php echo $form->dropdownlist ($model, ' Grade ', Array (0=>)--Please select-",1=>" level 2010,2=> "Level 2011",3=> "Level 2012")); > <?php echo $form->error ($model, ' grade ');? > <br> <?php echo $form->labelex ($model, ' hobby ');? > <?php echo $form->checkboxlist ($model, ' hobby ', Array (1=> "basketball",2=> "soccer",3=> "volleyball"), Array ("Separator "=>" ));? > <?php echo $form->error ($model, ' hobby ');? > <!--submit--> <?php echo Chtml::submitbutton (' submit ');?
> <?php $this->endwidget ();?>
The code for the
model layer is as follows: user.php
<?php * * To change this template, choose Tools |
Templates * and open the template in the editor. */class User extends cactiverecord{//Create a Model object static public $password 2;//a non-database field, but the public static function is required in view
Model ($className = __class__) {return Parent::model ($className);
//Returns the name of the current datasheet public Function tablename () {//parent::tablename (); Return ' {{user} ';//This write can omit prefix}//Set label display name public function Attributelabels () {return Array ("username" =& gt; " Name "," "Password" => "password", "Password2" => "Confirm password", "email" => "Mailbox", "Sex" => "sex", "grade" =
> "Grade", "hobby" => "hobby",);
//Verify Form field public function rules () {return Array ("username", "required", "message" => "user name cannot be empty"), Array ("Password", "required", "message" => "Password cannot be empty"),//Authentication password and confirmation password array ("Password2", "compare", "Compareattribu TE "=>" "Password", "message" => "two times password Inconsistent"),//Verify email Array ("email", "email")"," AllowEmpty "=>false," message "=>" Mailbox format is incorrect "),//Verify grade Array (" Grade "," in "," Range "=>array (1,2,3)," message
"=>" Please select grade),//validation hobby, using custom rule array ("hobby", "Checkhobby"),);
}//Verify Hobby function Checkhobby () {$this->hobby;
$len = strlen ($this->hobby);
if ($len <3) {$this->adderror ("hobby", "hobby at least 2");
}}}?>
The code for the control layer is as follows:
Information add Add Display page and add processing use the same method public
function Actionadd () {
$user =new user ();
if (Isset ($_post[' user ')) {
/ foreach ($_post[' user '] as $k => $v) {
// $user-> $k = $v; }
if (Is_array ($_post[' user ' [' hobby ']))
$_post[' user ' [' Hobby ']= implode ' (', ', $_post[' user '] [' Hobby ']);
Print_r ($_post[' User ');
$user->attributes=$_post[' user '];
if ($user->save ()) {
echo ' success ';
} else{
echo ' error ';
}
$this->renderpartial ("New", Array ("model" => $user));
In which, the form of automatic verification, implemented in the model layer, the form element name and the database in the field name consistent, you can set the error message to prompt.
I hope this article will help you with the PHP program design based on the YII framework.