Table of Contents
yii user registration form verification example,
Articles you may be interested in:
Home Backend Development PHP Tutorial yii user registration form verification example, _PHP tutorial

yii user registration form verification example, _PHP tutorial

Jul 12, 2016 am 09:02 AM
yii form verify

yii user registration form verification example,

The example in this article describes the implementation method of yii user registration form verification. Share it with everyone for your reference, the details are as follows:

View layer: register.php

<&#63;php
//使用小物件生成form元素
$form=$this->beginWidget('CActiveForm');
&#63;>
<!--用户名-->
<&#63;php echo $form->labelEx($model,'username');&#63;>
<&#63;php echo $form->textField($model,'username');&#63;>
<&#63;php echo $form->error($model,'username');&#63;>
<br>
<!--密码-->
<&#63;php echo $form->labelEx($model,'password');&#63;>
<&#63;php echo $form->passwordField($model,'password');&#63;>
<&#63;php echo $form->error($model,'password');&#63;>
<br>
<!--确认密码-->
<&#63;php echo $form->labelEx($model,'password2');&#63;>
<&#63;php echo $form->passwordField($model,'password2');&#63;>
<&#63;php echo $form->error($model,'password2');&#63;>
<br>
<!--邮箱-->
<&#63;php echo $form->labelEx($model,'email');&#63;>
<&#63;php echo $form->textField($model,'email');&#63;>
<&#63;php echo $form->error($model,'email');&#63;>
<br>
<!--性别-->
<&#63;php echo $form->labelEx($model,'sex');&#63;>
<&#63;php echo $form->radioButtonList($model,'sex',array(1=>'男',2=>'女'),array("separator"=>" "));&#63;>
<br>
<!--年级-->
<&#63;php echo $form->labelEx($model,'grade');&#63;>
<&#63;php echo $form->dropDownList($model,'grade',array(0=>"--请选择--",1=>"2010级",2=>"2011级",3=>"2012级"));&#63;>
<&#63;php echo $form->error($model,'grade');&#63;>
<br>
<&#63;php echo $form->labelEx($model,'hobby');&#63;>
<&#63;php echo $form->checkBoxList($model,'hobby',array(1=>"篮球",2=>"足球",3=>"排球"),array("separator"=>" "));&#63;>
<&#63;php echo $form->error($model,'hobby');&#63;>
<!--提交-->
<&#63;php echo CHtml::submitButton('提交');&#63;>
<&#63;php
$this->endWidget();
&#63;>

Copy after login

The code of the model layer is as follows: User.php

<&#63;php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class User extends CActiveRecord{
  //创建一个模型的对象 静态
  public $password2;//非数据库的字段,但是在view中需要用到
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }
  //返回当前数据表的名字
  public function tableName() {
    //parent::tableName();
    return '{{user}}';//这样写可以省略前缀
  }
  //设置标签的显示名字
  public function attributeLabels() {
    return array(
      "username"=>"姓名",
      "password"=>"密码",
      "password2"=>"确认密码",
      "email"=>"邮箱",
      "sex"=>"性别",
      "grade"=>"年级",
      "hobby"=>"爱好",
    );
  }
  //验证表单域
  public function rules() {
    return array(
      array("username","required","message"=>"用户名不能为空"),
      array("password","required","message"=>"密码不能为空"),
      //验证密码和确认密码
      array("password2","compare","compareAttribute"=>"password","message"=>"两次密码不一致"),
      //验证邮箱
      array("email","email","allowEmpty"=>false,"message"=>"邮箱格式不正确"),
      //验证年级
      array("grade","in","range"=>array(1,2,3),"message"=>"请选择年级"),
      //验证爱好,使用自定义规则
      array("hobby","checkHobby"),
    );
  }
  //验证爱好
  function checkHobby(){
    $this->hobby;
    $len= strlen($this->hobby);
    if($len<3){
      $this->addError("hobby","爱好至少为2个以上");
    }
  }
}
&#63;>

Copy after login

The code of the control layer is as follows:

//信息添加 添加显示页面和添加处理使用同一个方法
  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));
}

Copy after login

Among them, the automatic verification of the form is implemented in the model layer. The names of the form elements are consistent with the field names in the database, and error messages can be set to prompt.

I hope this article will be helpful to everyone’s PHP program design based on the yii framework.

Articles you may be interested in:

  • Comprehensive form validation rules of PHP Yii framework
  • Yii framework form usage examples
  • Yii does not rely on Model Examples of usage of form generator
  • Examples of using yii framework form model and submitting form data in array form
  • Yii2.0 implements the verification of username and email function
  • PHP YII framework development Tips: custom validation rules for rules in models
  • Yii uses ajax verification to display error messagebox solutions
  • yii implements creation of verification code instance analysis
  • in yii Detailed explanation of the method of adding a new user verification in

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1084514.htmlTechArticleyii user registration form verification example. This example describes the implementation method of yii user registration form verification. Share it with everyone for your reference, the details are as follows: View layer: register.php php//use...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to verify signature in PDF How to verify signature in PDF Feb 18, 2024 pm 05:33 PM

We usually receive PDF files from the government or other agencies, some with digital signatures. After verifying the signature, we see the SignatureValid message and a green check mark. If the signature is not verified, the validity is unknown. Verifying signatures is important, let’s see how to do it in PDF. How to Verify Signatures in PDF Verifying signatures in PDF format makes it more trustworthy and the document more likely to be accepted. You can verify signatures in PDF documents in the following ways. Open the PDF in Adobe Reader Right-click the signature and select Show Signature Properties Click the Show Signer Certificate button Add the signature to the Trusted Certificates list from the Trust tab Click Verify Signature to complete the verification Let

How to implement page jump after PHP form submission How to implement page jump after PHP form submission Aug 12, 2023 am 11:30 AM

How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

Detailed method to unblock using WeChat friend-assisted verification Detailed method to unblock using WeChat friend-assisted verification Mar 25, 2024 pm 01:26 PM

1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

New features in PHP 8: Added verification and signing New features in PHP 8: Added verification and signing Mar 27, 2024 am 08:21 AM

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

How to validate IFSC code using regular expressions? How to validate IFSC code using regular expressions? Aug 26, 2023 pm 10:17 PM

Indian Financial System Code is the abbreviation. Indian bank branches participating in the electronic funds transfer system are identified by a special 11-character code. The Reserve Bank of India uses this code in internet transactions to transfer funds between banks. IFSC code is divided into two parts. Banks are identified by the first four characters, while branches are identified by the last six characters. NEFT (National Electronic Funds Transfer), RTGS (Real Time Gross Settlement) and IMPS (Immediate Payment Service) are some of the electronic transactions that require IFSC codes. Method Some common ways to validate IFSC codes using regular expressions are: Check if the length is correct. Check the first four characters. Checkthefifthcharacter.Che

How to handle user rights management in PHP forms How to handle user rights management in PHP forms Aug 10, 2023 pm 01:06 PM

How to handle user rights management in PHP forms With the continuous development of web applications, user rights management is one of the important functions. User rights management can control users' operating rights in applications and ensure the security and legality of data. In PHP forms, user rights management can be implemented through some simple code. This article will introduce how to handle user rights management in PHP forms and give corresponding code examples. 1. Definition and management of user roles First of all, defining and managing user roles is a matter of user rights.

How to use JavaScript to implement real-time verification of the input box content of a form? How to use JavaScript to implement real-time verification of the input box content of a form? Oct 18, 2023 am 08:47 AM

How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML

How to use JavaScript to realize the automatic prompt function of the input box content of the form? How to use JavaScript to realize the automatic prompt function of the input box content of the form? Oct 20, 2023 pm 04:01 PM

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: &lt;!DOCTYP

See all articles