Class CActiveForm
CActiveForm provides a set of methods that can help to simplify the creation of complex and interactive HTML forms that are associated with data models.
The 'beginWidget' and 'endWidget' call of CActiveForm widget will render the
open and close form tags. Most other methods of CActiveForm are wrappers of the
corresponding 'active' methods in CHtml
. Calling them in between the
'beginWidget' and 'endWidget' calls will render text labels, input fields, etc.
For example, calling CActiveForm::textField()
would generate an input
field for a specified model attribute.
- server-side validation: the validation is performed at server side after the whole page containing the form is submitted. If there is any validation error, CActiveForm will render the error in the page back to user.
- AJAX-based validation: when the user enters data into an input field, an AJAX request is triggered which requires server-side validation. The validation result is sent back in AJAX response and the input field changes its appearance accordingly.
- client-side validation (available since version 1.1.7): when the user enters data into an input field, validation is performed on the client side using JavaScript. No server contact will be made, which reduces the workload on the server.
All these validations share the same set of validation rules declared in the associated model class. CActiveForm is designed in such a way that all these validations will lead to the same user interface changes and error message content.
To ensure data validity, server-side validation is always performed. By
setting CActiveForm::$enableAjaxValidation
to true, one can enable AJAX-based
validation; and by setting CActiveForm::$enableClientValidation
to true, one can
enable client-side validation. Note that in order to make the latter two
validations work, the user's browser must has its JavaScript enabled. If not,
only the server-side validation will be performed.
The AJAX-based validation and client-side validation may be used together or separately. For example, in a user registration form, one may use AJAX-based validation to check if the user has picked a unique username, and use client-side validation to ensure all required fields are entered with data. Because the AJAX-based validation may bring extra workload on the server, if possible, one should mainly use client-side validation.
The AJAX-based validation has a few limitations. First, it does not work with file upload fields. Second, it should not be used to perform validations that may cause server-side state changes. Third, it is not designed to work with tabular data input for the moment.
Support for client-side validation varies for different validators. A validator will support client-side validation only if it implementsCValidator::clientValidateAttribute()
and has its CValidator::$enableClientValidation
property set true. At this moment, the
following core validators support client-side validation:
CBooleanValidator
CCaptchaValidator
CCompareValidator
CEmailValidator
CNumberValidator
CRangeValidator
CRegularExpressionValidator
CRequiredValidator
CStringValidator
CUrlValidator
CActiveForm relies on CSS to customize the appearance of input fields which
are in different validation states. In particular, each input field may be one
of the four states: initial (not validated), validating, error and success. To
differentiate these states, CActiveForm automatically assigns different CSS
classes for the last three states to the HTML element containing the input
field. By default, these CSS classes are named as 'validating', 'error' and
'success', respectively. We may customize these CSS classes by configuring the
CActiveForm::$clientOptions
property or specifying in the CActiveForm::error()
method.
The following is a piece of sample view code showing how to use CActiveForm:
<?php $form = $this->beginWidget('CActiveForm', array( 'id'=>'user-form', 'enableAjaxValidation'=>true, 'enableClientValidation'=>true, 'focus'=>array($model,'firstName'), )); ?> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'firstName'); ?> <?php echo $form->textField($model,'firstName'); ?> <?php echo $form->error($model,'firstName'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'lastName'); ?> <?php echo $form->textField($model,'lastName'); ?> <?php echo $form->error($model,'lastName'); ?> </div> <?php $this->endWidget(); ?>
To respond to the AJAX validation requests, we need the following class code:
public function actionCreate() { $model=new User; $this->performAjaxValidation($model); if(isset($_POST['User'])) { $model->attributes=$_POST['User']; if($model->save()) $this->redirect('index'); } $this->render('create',array('model'=>$model)); } protected function performAjaxValidation($model) { if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') { echo CActiveForm::validate($model); Yii::app()->end(); } }
In the above code, if we do not enable the AJAX-based validation, we can
remove the performAjaxValidation
method and its invocation.
- CComponent
- CBaseController
- CWidget
- CActiveForm
Direct known subclasses
X2ActiveFormIndirect known subclasses
ActionActiveForm, ActionActiveFormBase, CalendarEventActiveForm, CallActiveForm, EventActiveForm, MobileActiveForm, NoteActiveForm, TimeActiveFormCopyright: 2008-2013 Yii Software LLC
License: http://www.yiiframework.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 1.1.1
Located at x2engine/framework/web/widgets/CActiveForm.php
public
|
|
public
|
|
public
string
|
#
error(
Displays the first validation error for a model attribute. This is similar to
|
public
string
|
#
errorSummary( mixed $models, string $header = null, string $footer = null, array $htmlOptions = array() )
Displays a summary of validation errors for one or several models. This
method is very similar to |
public
string
|
#
label(
Renders an HTML label for a model attribute. This method is a wrapper of
|
public
string
|
#
labelEx(
Renders an HTML label for a model attribute. This method is a wrapper of
|
public
string
|
#
urlField(
Renders a url field for a model attribute. This method is a wrapper of |
public
string
|
#
emailField(
Renders an email field for a model attribute. This method is a wrapper of
|
public
string
|
#
numberField(
Renders a number field for a model attribute. This method is a wrapper of
|
public
string
|
#
rangeField(
Generates a range field for a model attribute. This method is a wrapper of
|
public
string
|
#
dateField(
Renders a date field for a model attribute. This method is a wrapper of
|
public
string
|
#
timeField(
Renders a time field for a model attribute. This method is a wrapper of
|
public
string
|
#
dateTimeField(
Renders a datetime field for a model attribute. This method is a wrapper of
|
public
string
|
#
dateTimeLocalField(
Renders a local datetime field for a model attribute. This method is a
wrapper of |
public
string
|
#
weekField(
Renders a week field for a model attribute. This method is a wrapper of
|
public
string
|
#
colorField(
Renders a color picker field for a model attribute. This method is a wrapper
of |
public
string
|
#
telField(
Renders a tel field for a model attribute. This method is a wrapper of |
public
string
|
#
textField(
Renders a text field for a model attribute. This method is a wrapper of
|
public
string
|
#
searchField(
Renders a search field for a model attribute. This method is a wrapper of
|
public
string
|
#
passwordField(
Renders a password field for a model attribute. This method is a wrapper of
|
public
string
|
#
textArea(
Renders a text area for a model attribute. This method is a wrapper of |
public
string
|
#
fileField(
Renders a file field for a model attribute. This method is a wrapper of
|
public
string
|
#
radioButton(
Renders a radio button for a model attribute. This method is a wrapper of
|
public
string
|
#
checkBox(
Renders a checkbox for a model attribute. This method is a wrapper of |
public
string
|
#
dropDownList(
Renders a dropdown list for a model attribute. This method is a wrapper of
|
public
string
|
#
listBox(
Renders a list box for a model attribute. This method is a wrapper of |
public
string
|
#
checkBoxList(
Renders a checkbox list for a model attribute. This method is a wrapper of
|
public
string
|
#
radioButtonList(
Renders a radio button list for a model attribute. This method is a wrapper
of |
public static
string
|
|
public static
string
|
#
validateTabular( mixed $models, array $attributes = null, boolean $loadInput = true )
Validates an array of model instances and returns the results in JSON format. This is a helper method that simplifies the way of writing AJAX validation code for tabular input. |
__construct(),
actions(),
getController(),
getId(),
getOwner(),
getViewFile(),
getViewPath(),
render(),
setId()
|
beginCache(),
beginClip(),
beginContent(),
beginWidget(),
createWidget(),
endCache(),
endClip(),
endContent(),
endWidget(),
renderFile(),
renderInternal(),
widget()
|
public
mixed
|
$action | '' |
#
the form action URL (see |
public
string
|
$method | 'post' |
#
the form submission method. This should be either 'post' or 'get'. Defaults to 'post'. |
public
boolean
|
$stateful | false |
#
whether to generate a stateful form (See |
public
string
|
$errorMessageCssClass |
|
#
the CSS class name for error messages. Since 1.1.14 this defaults to
'errorMessage' defined in |
public
array
|
$htmlOptions | array() |
#
additional HTML attributes that should be rendered for the form tag. |
public
array
|
$clientOptions | array() |
#
the options to be passed to the javascript validation plugin. The following
options are supported:
Some of the above options may be overridden in individual calls of |
public
boolean
|
$enableAjaxValidation | false |
#
whether to enable data validation via AJAX. Defaults to false. When this property is set true, you should respond to the AJAX validation request on the server side as shown below: public function actionCreate() { $model=new User; if(isset($_POST['ajax']) && $_POST['ajax']==='user-form') { echo CActiveForm::validate($model); Yii::app()->end(); } ...... } |
public
boolean
|
$enableClientValidation | false |
#
whether to enable client-side data validation. Defaults to false. When this property is set true, client-side validation will be performed by
validators that support it (see |
public
mixed
|
$focus |
|
#
form element to get initial input focus on page load. Defaults to null meaning no input field has a focus. If set as array, first element should be model and second element should be the attribute. If set as string any jQuery selector can be used Example - set input focus on page load to:
|
protected
array
|
$attributes | array() |
#
the javascript options for model attributes (input ID => options) |
protected
string
|
$summaryID |
|
#
the ID of the container element for error summary |
$actionPrefix,
$skin
|
$controller,
$id,
$owner,
$viewPath
|