Home  >  Article  >  Backend Development  >  PHP development framework Yii Framework tutorial (10) UI components custom components

PHP development framework Yii Framework tutorial (10) UI components custom components

黄舟
黄舟Original
2017-01-21 10:05:181101browse

Before introducing Yii's built-in UI components, let's first introduce how to customize the components. This will also help to understand the usage of CWidget. Customizing the component is to overload the init() and run() methods of CWidget.

class MyWidget extends CWidget{public function init(){// 此方法会被 CController::beginWidget() 调用}
public function run(){// 此方法会被 CController::endWidget() 调用}}

This example defines a value range input UI component-RangeInputField by extending CInputWidget, which allows the user to enter two numbers to define a value range. CInputWidget supports using CModel or directly using variables, and RangeInputField also retains this tradition.

RangeInputField defines three sets of properties.

$attributeFrom and $attributeTo are used in CModel. With the activeXXX method of CHtml, activeXXX can automatically generate the label and text box of the text box.

Properties $nameFrom, $nameTo, $valueFrom, $valueTo programmers can define the label of the text box by themselves.

According to the default directory structure of the Yii application, the newly created RangeInputField is placed in the protected/components directory, so create protected/components/RangeInputField.php

class RangeInputField extends CInputWidget{public $attributeFrom;public $attributeTo;
public $nameFrom;public $nameTo;
public $valueFrom;public $valueTo;
function run(){if($this->hasModel()){
echo CHtml::activeTextField($this->model,
$this->attributeFrom);echo ' -> ';
echo CHtml::activeTextField($this->model,
$this->attributeTo);}else{echo CHtml::textField($this->nameFrom,$this->valueFrom);
echo ' -> ';echo CHtml::textField($this->nameTo,$this->valueTo);}}
/*** @return boolean whether this widget
* is associated with a data model.*/
protected function hasModel(){return $this->model instanceof CModel&& 
$this->attributeFrom!==null&& $this->attributeTo!==null;}}

This way you customize a new The UI component RangeInputField only overloads the run method, and uses the init method in its parent class.

Now you can test this newly created custom UI component RangeInputField. We use the FormModel (using CModel) method to use this UI component.

Create RangeFrom.php under protected/models

class RangeForm extends CFormModel{public $from;public $to;
function rules(){return 
array(array('from,to','numerical','integerOnly' =>true),
array(&#39;from&#39;,&#39;compare&#39;,&#39;compareAttribute&#39;=>&#39;to&#39;,&#39;operator&#39;=> &#39;<=&#39;,&#39;skipOnError&#39; => true),);}}

Then modify the default method of the default Controller, the actionIndex method in protected/controllers/siteController.php.

public function actionIndex(){$success=false;$model=new RangeForm();
if(!emptyempty($_POST[&#39;RangeForm&#39;])){$model->attributes=$_POST[&#39;RangeForm&#39;];if($model->validate()) $success=true;
}
$this->render(&#39;index&#39;, array(&#39;model&#39; => $model,&#39;success&#39; => $success,));}

Create the corresponding View

Success!
beginWidget(&#39;CActiveForm&#39;); ?>
errorSummary($model); ?>
widget(&#39;RangeInputField&#39;,array(&#39;model&#39;=>$model,&#39;attributeFrom&#39; => &#39;from&#39;,&#39;attributeTo&#39; => &#39;to&#39;,)) ?>endWidget(); ?>

Run this example

PHP development framework Yii Framework tutorial (10) UI components custom components

The above is the PHP development framework Yii Framework tutorial (10) UI component self- Define the content of the component. 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