Yii use Forms

Source: Internet
Author: User
: This article mainly introduces the use of Forms in Yii. if you are interested in the PHP Tutorial, refer to it. 1. create a model

A. add a base class

Use yii/base/Model

B. create a class that inherits from the base class

C. create required variables

E. define rules

F. Note that [] should be used in it.

For example:

  

This class inherits from a base class [[Yii \ base \ Model] provided by yii. this base class is usually used to represent data.

Supplement: [[yii \ base \ Model] is used as the parent class of the common Model class and has nothing to do with the data table. [[Yii \ db \ ActiveRecord] is usually the parent class of a common model class, but is associated with a data table: the [[yii \ db \ ActiveRecord] class actually inherits from [[yii \ base \ Model] and adds database processing ).

EntryFormClass inclusionnameAndemailTwo common members are used to store user input data. It also containsrules()Is used to return the set of data verification rules. The verification rules stated above indicate:

  • nameAndemailAll values are required.
  • emailThe value must meet the email rule verification

If you haveEntryFormObject. you can call its [[yii \ base \ Model: validate () | validate ()] method to trigger data verification. If data verification fails, set the [[yii \ base \ Model: hasErrors | hasErrors] attribute to true, if you want to know the specific error, call [[yii \ base \ Model: getErrors | getErrors].

  Name = 'Qiang '; $ model-> email = 'bad'; if ($ model-> validate () {// verification successful !} Else {// failed! // Use $ model-> getErrors () to obtain error details}

2. create operation

You havesiteCreate a controllerentryOperation is used to create a model. The creation and use of operations are explained in the "Hello" section.

  Load (Yii: $ app-> request-> post () & $ model-> validate ()) {// verify the data received by $ model // do something meaningful... return $ this-> render ('entry-confirm', ['model' => $ model]);} else {// whether it is an initialization display or data verification error return $ this-> render ('entry ', ['model' => $ model]) ;}}

This operation first createsEntryFormObject. Then try$_POSTCollect User-submitted data, which is collected by the Yii [[yii \ web \ Request: post ()] method. If the Model is successfully filled with data (that is, the user has submitted an HTML form), the operation will call [[yii \ base \ Model: validate () | validate ()] to ensure that the user submits valid data.

Supplement: expressionYii::$appRepresents an application instance, which is a globally accessible Singleton. It is also a service locator that providesrequest,response,dbAnd so on. In the above coderequestComponent to access$_POSTData.

After the user submits the form, the operation will renderentry-confirmView to confirm the user input data. If the form is not filled in, it is submitted, or the data contains errors (translator: if the email format is incorrect ),entryThe view will render the output, along with the details of the verification error output along with the form.

Note: In this simple example, we only present a confirmation page for valid data. In practice, you should consider using [[yii \ web \ Controller: refresh () | refresh ()] or [[yii \ web \ Controller: redirect () | redirect ()] to avoid repeated submission of forms.

3. create a view

Finally, create two view files.entry-confirmAndentry. They will be created byentryOperation rendering.

entry-confirmThe View displays the submitted name and email data. View files are stored inviews/site/entry-confirm.php.

  

You have entered the following information:

  • Name: name) ?>
  • Email: email) ?>

entryThe View displays an HTML form. View files are stored inviews/site/entry.php

  
      
  field($model, 'name') ?>    
  field($model, 'email') ?>    

'btn btn-primary']) ?>

The View uses a powerful widget [[yii \ widgets \ ActiveForm | ActiveForm] to generate HTML forms. Thebegin()Andend()Used to render the start and close labels of the form respectively. The [[yii \ widgets \ ActiveForm: field () | field ()] method is used between the two methods to create an input box. The first input box is used for "name", and the second input box is used for "email ". Then use [[yii \ helpers \ Html: submitButton ()] to generate the submit button.

use yii\helpers\Html;use yii\wigets\ActiveForm;

Remember to introduce the two widgets

The preceding section describes how to use Forms in Yii, including related content. if you are interested in the PHP Tutorial.

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.