PHP development framework YiiFramework tutorial (10) UI component custom component
Before introducing the Yii built-in UI components, we will first introduce how to customize the components. This will also help you understand the usage of CWidget. the custom components are the init () and run () methods that reload CWidget.
Class MyWidget extends CWidget
{
Public function init ()
{
// This method will be called by CController: beginWidget ()
}
Public function run ()
{
// This method will be called by CController: endWidget ()
}
} In this example, a value field input UI component-RangeInputField is defined by extending CInputWidget, which allows users to enter two numbers to define a value field range. CInputWidget supports using CModel or directly Using Variables. RangeInputField also retains this tradition.
RangeInputField defines three attributes.
$ AttributeFrom and $ attributeTo are used for CModel. with the activeXXX method of CHtml, activeXXX can automatically generate the label and text box of the text box.
Attribute $ nameFrom, $ nameTo, $ valueFrom, $ valueTo programmers can customize the label of the text box.
According to the default directory structure of the Yii application, the newly created RangeInputField is placed in the protected/components Directory. Therefore, protected/components/RangeInputField. php is created.
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;
}
} In this way, a new UI component RangeInputField is customized, and only the run method is Reloaded. init uses the methods in its parent class.
Next we can test the newly created custom UI component RangeInputField. We use FormModel (using CModel) to use this UI component.
Create RangeFrom. php under protected/models
Class RangeForm extends CFormModel
{
Public $ from;
Public $;
Function rules ()
{
Return array (
Array ('from, to ', 'numerical', 'integeronly' => true ),
Array ('from', 'Company', 'companyattribute '=> 'to ',
'Operator' => '<=', 'skiponerror' => true ),
);
}
} Then modify the default Controller method, namely the actionIndex method in protected/controllers/siteController. php.
Public function actionIndex ()
{
$ Success = false;
$ Model = new RangeForm ();
If (! Emptyempty ($ _ POST ['rangeid'])
{
$ Model-> attributes = $ _ POST ['rangeid'];
If ($ model-> validate () $ success = true;
}
$ This-> render ('index', array (
'Model' => $ model,
'Success '=> $ success,
));
}
Create a View
Success!
BeginWidget ('cactiveform');?>
ErrorSummary ($ model);?>
Widget ('rangeinputfield ', array (
'Model' => $ model,
'Bubutefrom' => 'from ',
'Butbuteto '=> 'to ',
)?>
EndWidget ();?>
Run this example