Yii User Registration Form Validation example, _php tutorial

Source: Internet
Author: User
Tags array example

Yii User Registration Form validation instance,


This paper describes the implementation method of Yii user registration form verification. Share to everyone for your reference, as follows:

View Layer: register.php

<?php//uses small objects to generate form elements $form= $this->beginwidget (' cactiveform ');? >
 <?php echo $form->labelex ($model, ' username ');? ><?php echo $form->textfield ($model, ' username ');? ><?php echo $form->error ($model, ' username ');? >
<?php echo $form->labelex ($model, ' password ');? ><?php echo $form->passwordfield ($model, ' password ');? ><?php echo $form->error ($model, ' password ');? >
<?php echo $form->labelex ($model, ' password2 ');? ><?php echo $form->passwordfield ($model, ' password2 ');? ><?php echo $form->error ($model, ' password2 ');? >
<?php echo $form->labelex ($model, ' email ');? ><?php echo $form->textfield ($model, ' email ');? ><?php echo $form->error ($model, ' email ');? >
<?php echo $form->labelex ($model, ' sex ');? ><?php echo $form->radiobuttonlist ($model, ' sex ', array (1=> ' male ',2=> ' female '), Array ("separator" + ")");? >
<?php echo $form->labelex ($model, ' grade ');? ><?php echo $form->dropdownlist ($model, ' Grade ', Array (0=> "--Please select--",1=> "Level 2010",2=> "2011",3=> "Level 2012")); ><?php echo $form->error ($model, ' grade ');? >
<?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 ');? > <?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;//non-database fields, but public static function mode is required in view  L ($className = __class__) {return Parent::model ($className);    }//Returns the name of the current data table public function TableName () {//parent::tablename (); Return ' {user} ';//This can omit the prefix}//Set the display name of the label public Function Attributelabels () {return Array ("username" = "surname" Name "," "Password" and "password", "Password2" and "Confirm password", "email" and "Mailbox", "Sex" and "gender", "Grade" and "year"  Level "," hobby "and" hobby ",); }//Verify form field public function rules () {return Array ("username", "required", "message" = "User name cannot be empty"), arr Ay ("password", "required", "message" = "Password cannot be empty"),//Verify password and Confirm password Array ("Password2", "compare", "Compareattribute" =&gt ;" Password "," message "=" two times password Inconsistent "),//Verify mailbox Array (" email "," email "," AllowEmpty "=>false," message "=&Gt; " Incorrect mailbox format "),//Verify grade Array (" Grade "," in "," range "=>array (All-in-all)," message "=" Please select Grade "),//verification hobby, using custom rule a  Rray ("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 using 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));

Where the automatic validation of forms, implemented in the model layer, the name of the form element is consistent with the name of the field in the database, you can set the error message to prompt.

It is hoped that this article is helpful to the PHP program design based on YII framework.

Articles you may be interested in:

    • PHP Yii Framework's form validation rules Daquan
    • Yii Framework Form Form Usage example
    • Yii Form Builder usage instances that do not rely on model
    • Yii framework form model using and submitting form data as an array example
    • yii2.0 implementing authentication for user name and mailbox features
    • PHP YII Framework Development Little tricks Model (models) Rules custom validation rule
    • Yii uses AJAX validation to display error MessageBox solutions
    • Yii implementation to create verification code instance resolution
    • Add a new user authentication method in Yii

http://www.bkjia.com/PHPjc/1084514.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084514.html techarticle yii User Registration Form Verification example, this article describes the Yii user Registration form verification implementation method. Share to everyone for reference, as follows: View layer: register.php php//make ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.