PHP development framework YiiFramework tutorial (10) UI component custom component

Source: Internet
Author: User
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

 


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.