Yii Framework official guide series 19-use forms: create forms

Source: Internet
Author: User
Writing the login view is very simple. we start with a form mark, and its action attribute should be the URL of the login action described earlier. Then we need...



Writing the login view is very simple. we start with a form mark, and its action attribute should be the URL of the login action described earlier. Then we need to insert tags and form fields for the attributes declared in the LoginForm class. Finally, we insert a button that allows users to click to submit this form. All of these can be done with pure HTML code.

Yii provides several helper (helper) classes to simplify view writing. For example, to create a text input field, call CHtml: textField (); to create a drop-down list, call CHtml: dropDownList ().

Information:You may want to know the benefits of using assistants, if they require the equivalent amount of code to directly write pure HTML code. The answer is that the assistant can provide more functions than HTML code. For example, the following code generates a text input field, which can trigger the form submission action when the user modifies its value.


CHtml: textField ($ name, $ value, array ('submit '=> ''));

Otherwise, you need to write a lot of JavaScript.

Next, we use CHtml to create a login form. Assume that the variable $ model is an instance of LoginForm.


The above code generates a more dynamic form, for example, CHtml: activeLabel () to generate a tag related to the features of the specified model. If an input error occurs for this feature, the CSS class of the label changes to error, and the appearance of the label is changed through the CSS style. Similarly, CHtml: activeTextField () generates a text input field for the features of the specified model and changes its CSS class when an error occurs.

If we use the CSS style file provided by yiic script, the generated form will be like the following:

From version 1.1.1, a new small object CActiveForm is provided to simplify form creation. This small object provides seamless and consistent verification between the client and the server. With CActiveForm, the above code can be rewritten:

beginWidget('CActiveForm'); ?> errorSummary($model); ?>

label($model,'username'); ?> textField($model,'username') ?>

label($model,'password'); ?> passwordField($model,'password') ?>

checkBox($model,'rememberMe'); ?> label($model,'rememberMe'); ?>

endWidget(); ?>


The above is the Yii Framework official guide series 19-use form: Create form content. For more information, see PHP Chinese website (www.php1.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.