Before introducing the Yii built-in UI components, let's look at how to customize the components, which also helps to understand the use of cwidget, which is the Init () and run () method of overloading CWidget.
Class Mywidget extends Cwidget{public function init () {//This method is called by Ccontroller::beginwidget ()}public function Run () {//This method will be Ccontroller::endwidget () called}}
In this example, by extending cinputwidget, a domain input UI component-rangeinputfield is defined, which allows the user to enter two numbers to define a range of ranges. Cinputwidget supports the use of Cmodel or the use of variables directly, Rangeinputfield also retains this tradition.
Rangeinputfield defines three sets of properties.
$attributeFrom and $attributeTo for Cmodel, with the Activexxx method of cHTML, activexxx can automatically generate labels and text boxes for text boxes.
Properties $namefrom, $nameTo, $valueFrom, $valueTo programmers can define their own labels for text boxes.
According to the default directory structure of the YII application, the newly created Rangeinputfield is placed in the Protected/components directory and therefore created protected/components/rangeinputfield.php
Class Rangeinputfield extends Cinputwidget{public $attributeFrom;p ublic $attributeTo;p ublic $nameFrom;p ublic $nameTo; Public $valueFrom;p ublic $valueTo, function run () {if ($this->hasmodel ()) {echo Chtml::activetextfield ($this Model, $this->attributefrom); Echo Chtml::activetextfield ($this->model, $this->attributeto);} Else{echo Chtml::textfield ($this->namefrom, $this->valuefrom); Echo Chtml::textfield ($this, Nameto, $this->valueto);}} /*** @return Boolean Whether this widget* was associated with a data model.*/protected function Hasmodel () {return $this-> Model instanceof cmodel&& $this->attributefrom!==null&& $this->attributeto!==null;}}
This customizes a new UI component, Rangeinputfield, that only overloads the Run method, and Init uses the method in its parent class.
Here you can test the newly created custom UI component Rangeinputfield, using the Formmodel (Cmodel) method to use this UI component.
Create a rangefrom.php under Protected/models
Class Rangeform extends Cformmodel{public $from;p ublic $to, function rules () {return array (' from,to ', ' numerical ', ' Integeronly ' =>true), Array (' from ', ' compare ', ' compareattribute ' = ' to ', ' operator ' = ' <= ', ' Skiponerror ' = true),);}}
Then modify the default controller defaults method, protected/controllers/sitecontroller.php the Actionindex method.
Public Function Actionindex () {$success =false; $model =new rangeform (); if (!emptyempty ($_post[' rangeform ')) {$model- >attributes=$_post[' Rangeform '];if ($model->validate ()) $success =true;} $this->render (' index ', array (' model ' = $model, ' success ' + $success,));}
Create a corresponding view
Success!beginwidget (' Cactiveform ');? >errorsummary ($model);? >widget (' Rangeinputfield ', Array (' model ' =>$ Model, ' attributefrom ' = ' from ', ' attributeto ' = ' to ', ')? >endwidget ();?>
Run this example
The above is the PHP Development Framework Yii Framework Tutorial (Ten) UI component custom component content, more relevant content please follow topic.alibabacloud.com (www.php.cn)!