PHP Development Framework Yii Framework Tutorial (one-by-one) UI component ActiveForm Example

Source: Internet
Author: User
The previous introduction to the YII Framework Development tutorial (7) using cHTML to create a form and Yii Framework Development Tutorial (8) using Formmodel has already made a brief introduction to Cactiveform. It will be Christmas in a few days, here to help Santa make a survey, to investigate the Christmas gifts everyone wants and the food they want to eat at Christmas dinner.

Here to share part of the code, as a follow-up example of the framework, basically copy Hello World example, add an empty Datamodel, Using the default controller (Sitecontroller) and the default action (Actionindex), use the default configuration file, code download.

Collecting user data through HTML forms is one of the main tasks of Web program development. In addition to the form design, developers need to populate the form with existing or default data, validate user input, display appropriate error messages for invalid input, and save input to persistent memory. Yii simplifies this workflow greatly through its MVC structure.

When working with forms in Yii, the following steps are usually required:

Create a model class to represent the data fields that you want to collect.

Creates a controller action that responds to form submission.

Create a form in the view script that is related to the controller action.

Create Datamodel

Classdatamodel extends Cformmodel{public $firstName;p ublic $lastName;p ublic $favouriteGift;p ublic $favouriteDinner; Public Function rules () {return Array (' FirstName, lastName ', ' required '), Array (' Favouritegift,favouritedinner ' , ' safe '));} Static $gifts =array (' 1 ' = ' iPad ', ' 2 ' = ' "Remote control Helicopter ', ' 3 ' + ' 3D LED TV ', ' 4 ' = ' Holy Bible ',); static $meals =array (' 1 ' = ' Egg ', ' 2 ' = ' Ham ', ' 3 ' = ' Chicken ', ' 4 ' = ' pork ', ' 5 ' = ' Beer ', ' 6 ' = > ' Coke ', ' 7 ' = ' Wine ',);}

Firstname,lastname is used to record the user's name, $favouriteGift and $favouritedinner to record the user's gift and food name. $gifts, $meals statically defines the types of gifts and food types that can be selected by the user. It is important to note that Firstname,lastname is required, and $favouriteGift and $favouritedinner are set to be safe to replicate, as defined by the model Rules.

Define Action

To modify the Sitecontroller action method:

Public Function Actionindex () {$model =new datamodel (); if (!emptyempty ($_post[' Datamodel '])) {$model->attributes=$ _post[' Datamodel '];if ($model->validate ()) {$this->render (' Choice ', Array (' model ' = = $model,); return;}} $this->render (' index ', array (' model ' = $model,));}

Here, two view,index are defined to obtain user input, choice display user selection results. $model->attributes=$_post [' Datamodel ']; As we said in the Secure attribute assignment, this line of code populates the model with user-submitted data. The Attributes property is defined by Cmodel, which accepts an array of name values and assigns each value to the corresponding model attribute.

Define View

The first is to define index.php, this example uses Cactiveform,cactiveform to provide both client and server-side seamless, consistent validation.

>beginwidget (' Cactiveform ', array (' id ' = ' user-form ', ' enableajaxvalidation ' =>true, ' Enableclientvalidation ' =>true, ' Focus ' =>array ($model, ' firstName '),);?>

This example is relatively simple and does not use these complex checks, only requires firstname,lastname non-null, which is defined by the rules in the Datamodel required.

Beginwidget (' Cactiveform ');?> errorsummary ($model);?> label ($model, ' firstName ');? >textfield ($model, ' FirstName ')?>  label ($model, ' lastName ');? >textfield ($model, ' lastName ')?>  Choose your Christmas Giftradiobuttonlist ($model, ' Favouritegift ', Datamodel:: $gifts)?>    Choose your Christmas dinnercheckboxlist ($ Model, ' Favouritedinner ', Datamodel:: $meals)?>     endwidget ();? >view Choice is simpler, showing user-selected results: Christmas ";?> FirstName. ' ' . $model->lastname. '.' ;? > You'll be Givenecho Datamodel:: $gifts [$model->favouritegift];? >on Christmas Day.  And you'll Haveforeach ($model->favouritedinner as $dinner) {echo Datamodel:: $meals [$dinner]. ' ';}? >for Christmas Dinner.


The results of the operation are as follows:

Vcyzzcgxmskgvunx6bz+iefjdgl2zuzvcm3kvsd9 "src="/uploadfile/2016/0106/20160106062558152.png "/>

The above is the content of the PHP Development Framework Yii Framework Tutorial (one) UI component ActiveForm sample, more relevant content please follow topic.alibabacloud.com (www.php.cn)!

  • 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.