Home  >  Article  >  Backend Development  >  PHP development framework Yii Framework tutorial (7) Create Form using CHtml

PHP development framework Yii Framework tutorial (7) Create Form using CHtml

黄舟
黄舟Original
2017-01-21 09:50:251166browse

When creating a View made of MVC in a Yii application, you can directly use HTML language to directly construct the page view. Yii also provides several helper classes to simplify view writing. For example, to create a text input field, we can call CHtml::textField(); to create a drop-down list, call CHtml::dropDownList(). Several Views in the previous Hangman examples used CHtml to create Views.

Information: You may wonder about the benefits of using help classes if they require the same amount of code as writing plain HTML directly. The answer is that helper classes can provide more functionality than HTML code. For example, the following code will generate a text input field that triggers a form submission action when the user modifies its value.

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

Otherwise you need to write a lot of JavaScript.

Recall the definition of page play in Hangman:

<p>This is the game of Hangman.     
You must guess a word, a letter at a time.     
If you make too many mistakes, you lose the game!</p>     
<?php echo CHtml::beginForm(); ?>     
<?php echo CHtml::radioButtonList(&#39;level&#39;, null, $levels); ?>     
<br/>     
<?php echo CHtml::submitButton(&#39;Play!&#39;); ?>     
<?php if($error): ?>     
<span style="color:red">You must choose a difficulty level!</span>     
<?php endif; ?>
<?php echo CHtml::endForm(); ?>

PHP development framework Yii Framework tutorial (7) Create Form using CHtml

Using CHtml to construct a page always starts with CHtml::beginForm() ::endForm() ends. In fact, these two methods generate the starting and ending tags of the From tag.

For UI components supported by HTML forms, such as button, radioButton, checkbutton and other UI components, the CHtml class provides corresponding methods, such as the above radioButtonList and submitButton.

In addition to radioButtonList, etc., CHtml also provides a set of activeXXX, such as activeRadioButtonList. These methods need to be used in conjunction with CFormModel. For examples like Hangman, we do not use another Model, so the activeRadioButtonList method is not used. I will introduce it later when I introduce the Model in MVC.

For all UI components supported by CHtml, please refer to the Yii class reference document: http://www.yiiframework.com/doc/api/1.1/CHtml

From version 1.1.1 Initially, a new widget CActiveForm. (http://www.yiiframework.com/doc/api/1.1/CActiveForm) is provided to simplify form creation. This little widget provides seamless, consistent validation on both the client and server sides. These are introduced together in the UI components supported by Yii.

The above is the content of the PHP development framework Yii Framework tutorial (7) using CHtml to create a Form. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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