Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • ActionActiveForm
  • ActionActiveFormBase
  • CActiveForm
  • CalendarEventActiveForm
  • CallActiveForm
  • CClipWidget
  • CContentDecorator
  • CFilterWidget
  • CFlexWidget
  • CHtmlPurifier
  • CInputWidget
  • CMarkdown
  • CMaskedTextField
  • CMultiFileUpload
  • COutputCache
  • COutputProcessor
  • CStarRating
  • CTabView
  • CTextHighlighter
  • CTreeView
  • CWidget
  • EventActiveForm
  • MobileActiveForm
  • NoteActiveForm
  • TimeActiveForm
  • X2ActiveForm
  • X2StarRating
  • Overview
  • Package
  • Class
  • Tree

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.

What makes CActiveForm extremely useful is its support for data validation. CActiveForm supports data validation at three levels:
  • 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 implements CValidator::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
Extended by CBaseController
Extended by CWidget
Extended by CActiveForm

Direct known subclasses

X2ActiveForm

Indirect known subclasses

ActionActiveForm, ActionActiveFormBase, CalendarEventActiveForm, CallActiveForm, EventActiveForm, MobileActiveForm, NoteActiveForm, TimeActiveForm
Package: system\web\widgets
Copyright: 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
Methods summary
public
# init( )

Initializes the widget. This renders the form open tag.

Initializes the widget. This renders the form open tag.

Overrides

CWidget::init()
public
# run( )

Runs the widget. This registers the necessary javascript code and renders the form close tag.

Runs the widget. This registers the necessary javascript code and renders the form close tag.

Overrides

CWidget::run()
public string
# error( CModel $model, string $attribute, array $htmlOptions = array(), boolean $enableAjaxValidation = true, boolean $enableClientValidation = true )

Displays the first validation error for a model attribute. This is similar to CHtml::error() except that it registers the model attribute so that if its value is changed by users, an AJAX validation may be triggered.

Displays the first validation error for a model attribute. This is similar to CHtml::error() except that it registers the model attribute so that if its value is changed by users, an AJAX validation may be triggered.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute name
$htmlOptions
array
$htmlOptions additional HTML attributes to be rendered in the container div tag. Besides all those options available in CHtml::error(), the following options are recognized in addition:
  • validationDelay
  • validateOnChange
  • validateOnType
  • hideErrorMessage
  • inputContainer
  • errorCssClass
  • successCssClass
  • validatingCssClass
  • beforeValidateAttribute
  • afterValidateAttribute
These options override the corresponding options as declared in options for this particular model attribute. For more details about these options, please refer to CActiveForm::$clientOptions. Note that these options are only used when CActiveForm::$enableAjaxValidation or CActiveForm::$enableClientValidation is set true.
  • inputID
When an CActiveForm input field uses a custom ID, for ajax/client validation to work properly inputID should be set to the same ID Example: <pre> <div class="form-element"> <?php echo $form->labelEx($model,'attribute'); ?> <?php echo $form->textField($model,'attribute', array('id'=>'custom-id')); ?> <?php echo $form->error($model,'attribute',array('inputID'=>'custom-id')); ?> </div> </pre> When client-side validation is enabled, an option named "clientValidation" is also recognized. This option should take a piece of JavaScript code to perform client-side validation. In the code, the variables are predefined:
  • value: the current input value associated with this attribute.
  • messages: an array that may be appended with new error messages for the attribute.
  • attribute: a data structure keeping all client-side options for the attribute
This should NOT be a function but just the code, Yii will enclose the code you provide inside the actual JS function.
$enableAjaxValidation
boolean
$enableAjaxValidation whether to enable AJAX validation for the specified attribute. Note that in order to enable AJAX validation, both CActiveForm::$enableAjaxValidation and this parameter must be true.
$enableClientValidation
boolean
$enableClientValidation whether to enable client-side validation for the specified attribute. Note that in order to enable client-side validation, both CActiveForm::$enableClientValidation and this parameter must be true. This parameter has been available since version 1.1.7.

Returns

string
the validation result (error display or success message).

See

CHtml::error()
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 CHtml::errorSummary() except that it also works when AJAX validation is performed.

Displays a summary of validation errors for one or several models. This method is very similar to CHtml::errorSummary() except that it also works when AJAX validation is performed.

Parameters

$models
mixed
$models the models whose input errors are to be displayed. This can be either a single model or an array of models.
$header
string
$header a piece of HTML code that appears in front of the errors
$footer
string
$footer a piece of HTML code that appears at the end of the errors
$htmlOptions
array
$htmlOptions additional HTML attributes to be rendered in the container div tag.

Returns

string
the error summary. Empty if no errors are found.

See

CHtml::errorSummary()
public string
# label( CModel $model, string $attribute, array $htmlOptions = array() )

Renders an HTML label for a model attribute. This method is a wrapper of CHtml::activeLabel(). Please check CHtml::activeLabel() for detailed information about the parameters for this method.

Renders an HTML label for a model attribute. This method is a wrapper of CHtml::activeLabel(). Please check CHtml::activeLabel() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated label tag
public string
# labelEx( CModel $model, string $attribute, array $htmlOptions = array() )

Renders an HTML label for a model attribute. This method is a wrapper of CHtml::activeLabelEx(). Please check CHtml::activeLabelEx() for detailed information about the parameters for this method.

Renders an HTML label for a model attribute. This method is a wrapper of CHtml::activeLabelEx(). Please check CHtml::activeLabelEx() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated label tag
public string
# urlField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a url field for a model attribute. This method is a wrapper of CHtml::activeUrlField(). Please check CHtml::activeUrlField() for detailed information about the parameters for this method.

Renders a url field for a model attribute. This method is a wrapper of CHtml::activeUrlField(). Please check CHtml::activeUrlField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.11
public string
# emailField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders an email field for a model attribute. This method is a wrapper of CHtml::activeEmailField(). Please check CHtml::activeEmailField() for detailed information about the parameters for this method.

Renders an email field for a model attribute. This method is a wrapper of CHtml::activeEmailField(). Please check CHtml::activeEmailField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.11
public string
# numberField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a number field for a model attribute. This method is a wrapper of CHtml::activeNumberField(). Please check CHtml::activeNumberField() for detailed information about the parameters for this method.

Renders a number field for a model attribute. This method is a wrapper of CHtml::activeNumberField(). Please check CHtml::activeNumberField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.11
public string
# rangeField( CModel $model, string $attribute, array $htmlOptions = array() )

Generates a range field for a model attribute. This method is a wrapper of CHtml::activeRangeField(). Please check CHtml::activeRangeField() for detailed information about the parameters for this method.

Generates a range field for a model attribute. This method is a wrapper of CHtml::activeRangeField(). Please check CHtml::activeRangeField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.11
public string
# dateField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a date field for a model attribute. This method is a wrapper of CHtml::activeDateField(). Please check CHtml::activeDateField() for detailed information about the parameters for this method.

Renders a date field for a model attribute. This method is a wrapper of CHtml::activeDateField(). Please check CHtml::activeDateField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.11
public string
# timeField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a time field for a model attribute. This method is a wrapper of CHtml::activeTimeField(). Please check CHtml::activeTimeField() for detailed information about the parameters for this method.

Renders a time field for a model attribute. This method is a wrapper of CHtml::activeTimeField(). Please check CHtml::activeTimeField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.14
public string
# dateTimeField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a datetime field for a model attribute. This method is a wrapper of CHtml::activeDateTimeField(). Please check CHtml::activeDateTimeField() for detailed information about the parameters for this method.

Renders a datetime field for a model attribute. This method is a wrapper of CHtml::activeDateTimeField(). Please check CHtml::activeDateTimeField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.16
public string
# dateTimeLocalField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a local datetime field for a model attribute. This method is a wrapper of CHtml::activeDateTimeLocalField(). Please check CHtml::activeDateTimeLocalField() for detailed information about the parameters for this method.

Renders a local datetime field for a model attribute. This method is a wrapper of CHtml::activeDateTimeLocalField(). Please check CHtml::activeDateTimeLocalField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.16
public string
# weekField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a week field for a model attribute. This method is a wrapper of CHtml::activeWeekField(). Please check CHtml::activeWeekField() for detailed information about the parameters for this method.

Renders a week field for a model attribute. This method is a wrapper of CHtml::activeWeekField(). Please check CHtml::activeWeekField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.16
public string
# colorField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a color picker field for a model attribute. This method is a wrapper of CHtml::activeColorField(). Please check CHtml::activeColorField() for detailed information about the parameters for this method.

Renders a color picker field for a model attribute. This method is a wrapper of CHtml::activeColorField(). Please check CHtml::activeColorField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.16
public string
# telField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a tel field for a model attribute. This method is a wrapper of CHtml::activeTelField(). Please check CHtml::activeTelField() for detailed information about the parameters for this method.

Renders a tel field for a model attribute. This method is a wrapper of CHtml::activeTelField(). Please check CHtml::activeTelField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.14
public string
# textField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a text field for a model attribute. This method is a wrapper of CHtml::activeTextField(). Please check CHtml::activeTextField() for detailed information about the parameters for this method.

Renders a text field for a model attribute. This method is a wrapper of CHtml::activeTextField(). Please check CHtml::activeTextField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field
public string
# searchField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a search field for a model attribute. This method is a wrapper of CHtml::activeSearchField(). Please check CHtml::activeSearchField() for detailed information about the parameters for this method.

Renders a search field for a model attribute. This method is a wrapper of CHtml::activeSearchField(). Please check CHtml::activeSearchField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field

Since

1.1.14
public string
# hiddenField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a hidden field for a model attribute. This method is a wrapper of CHtml::activeHiddenField(). Please check CHtml::activeHiddenField() for detailed information about the parameters for this method.

Renders a hidden field for a model attribute. This method is a wrapper of CHtml::activeHiddenField(). Please check CHtml::activeHiddenField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field
public string
# passwordField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a password field for a model attribute. This method is a wrapper of CHtml::activePasswordField(). Please check CHtml::activePasswordField() for detailed information about the parameters for this method.

Renders a password field for a model attribute. This method is a wrapper of CHtml::activePasswordField(). Please check CHtml::activePasswordField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated input field
public string
# textArea( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a text area for a model attribute. This method is a wrapper of CHtml::activeTextArea(). Please check CHtml::activeTextArea() for detailed information about the parameters for this method.

Renders a text area for a model attribute. This method is a wrapper of CHtml::activeTextArea(). Please check CHtml::activeTextArea() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated text area
public string
# fileField( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a file field for a model attribute. This method is a wrapper of CHtml::activeFileField(). Please check CHtml::activeFileField() for detailed information about the parameters for this method.

Renders a file field for a model attribute. This method is a wrapper of CHtml::activeFileField(). Please check CHtml::activeFileField() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes

Returns

string
the generated input field
public string
# radioButton( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a radio button for a model attribute. This method is a wrapper of CHtml::activeRadioButton(). Please check CHtml::activeRadioButton() for detailed information about the parameters for this method.

Renders a radio button for a model attribute. This method is a wrapper of CHtml::activeRadioButton(). Please check CHtml::activeRadioButton() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated radio button
public string
# checkBox( CModel $model, string $attribute, array $htmlOptions = array() )

Renders a checkbox for a model attribute. This method is a wrapper of CHtml::activeCheckBox(). Please check CHtml::activeCheckBox() for detailed information about the parameters for this method.

Renders a checkbox for a model attribute. This method is a wrapper of CHtml::activeCheckBox(). Please check CHtml::activeCheckBox() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated check box
public string
# dropDownList( CModel $model, string $attribute, array $data, array $htmlOptions = array() )

Renders a dropdown list for a model attribute. This method is a wrapper of CHtml::activeDropDownList(). Please check CHtml::activeDropDownList() for detailed information about the parameters for this method.

Renders a dropdown list for a model attribute. This method is a wrapper of CHtml::activeDropDownList(). Please check CHtml::activeDropDownList() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$data
array
$data data for generating the list options (value=>display)
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated drop down list
public string
# listBox( CModel $model, string $attribute, array $data, array $htmlOptions = array() )

Renders a list box for a model attribute. This method is a wrapper of CHtml::activeListBox(). Please check CHtml::activeListBox() for detailed information about the parameters for this method.

Renders a list box for a model attribute. This method is a wrapper of CHtml::activeListBox(). Please check CHtml::activeListBox() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$data
array
$data data for generating the list options (value=>display)
$htmlOptions
array
$htmlOptions additional HTML attributes.

Returns

string
the generated list box
public string
# checkBoxList( CModel $model, string $attribute, array $data, array $htmlOptions = array() )

Renders a checkbox list for a model attribute. This method is a wrapper of CHtml::activeCheckBoxList(). Please check CHtml::activeCheckBoxList() for detailed information about the parameters for this method.

Renders a checkbox list for a model attribute. This method is a wrapper of CHtml::activeCheckBoxList(). Please check CHtml::activeCheckBoxList() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$data
array
$data value-label pairs used to generate the check box list.
$htmlOptions
array
$htmlOptions addtional HTML options.

Returns

string
the generated check box list
public string
# radioButtonList( CModel $model, string $attribute, array $data, array $htmlOptions = array() )

Renders a radio button list for a model attribute. This method is a wrapper of CHtml::activeRadioButtonList(). Please check CHtml::activeRadioButtonList() for detailed information about the parameters for this method.

Renders a radio button list for a model attribute. This method is a wrapper of CHtml::activeRadioButtonList(). Please check CHtml::activeRadioButtonList() for detailed information about the parameters for this method.

Parameters

$model
CModel
$model the data model
$attribute
string
$attribute the attribute
$data
array
$data value-label pairs used to generate the radio button list.
$htmlOptions
array
$htmlOptions addtional HTML options.

Returns

string
the generated radio button list
public static string
# validate( mixed $models, array $attributes = null, boolean $loadInput = true )

Validates one or several models and returns the results in JSON format. This is a helper method that simplifies the way of writing AJAX validation code.

Validates one or several models and returns the results in JSON format. This is a helper method that simplifies the way of writing AJAX validation code.

Parameters

$models
mixed
$models a single model instance or an array of models.
$attributes
array
$attributes list of attributes that should be validated. Defaults to null, meaning any attribute listed in the applicable validation rules of the models should be validated. If this parameter is given as a list of attributes, only the listed attributes will be validated.
$loadInput
boolean
$loadInput whether to load the data from $_POST array in this method. If this is true, the model will be populated from <span class="php-var">$_POST</span>[ModelClass].

Returns

string
the JSON representation of the validation error messages.
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.

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.

Parameters

$models
mixed
$models an array of model instances.
$attributes
array
$attributes list of attributes that should be validated. Defaults to null, meaning any attribute listed in the applicable validation rules of the models should be validated. If this parameter is given as a list of attributes, only the listed attributes will be validated.
$loadInput
boolean
$loadInput whether to load the data from $_POST array in this method. If this is true, the model will be populated from <span class="php-var">$_POST</span>[ModelClass][<span class="php-var">$i</span>].

Returns

string
the JSON representation of the validation error messages.
Methods inherited from CWidget
__construct(), actions(), getController(), getId(), getOwner(), getViewFile(), getViewPath(), render(), setId()
Methods inherited from CBaseController
beginCache(), beginClip(), beginContent(), beginWidget(), createWidget(), endCache(), endClip(), endContent(), endWidget(), renderFile(), renderInternal(), widget()
Methods inherited from CComponent
__call(), __get(), __isset(), __set(), __unset(), asa(), attachBehavior(), attachBehaviors(), attachEventHandler(), canGetProperty(), canSetProperty(), detachBehavior(), detachBehaviors(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), getEventHandlers(), hasEvent(), hasEventHandler(), hasProperty(), raiseEvent()
Properties summary
public mixed $action ''
#

the form action URL (see CHtml::normalizeUrl() for details about this parameter). If not set, the current page URL is used.

the form action URL (see CHtml::normalizeUrl() for details about this parameter). If not set, the current page URL is used.

public string $method 'post'
#

the form submission method. This should be either 'post' or 'get'. Defaults to '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 CHtml::statefulForm()). Defaults to false.

whether to generate a stateful form (See CHtml::statefulForm()). Defaults to false.

public string $errorMessageCssClass
#

the CSS class name for error messages. Since 1.1.14 this defaults to 'errorMessage' defined in CHtml::$errorMessageCss. Individual CActiveForm::error() call may override this value by specifying the 'class' HTML option.

the CSS class name for error messages. Since 1.1.14 this defaults to 'errorMessage' defined in CHtml::$errorMessageCss. Individual CActiveForm::error() call may override this value by specifying the 'class' HTML option.

public array $htmlOptions array()
#

additional HTML attributes that should be rendered for the form tag.

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:
  • ajaxVar: string, the name of the parameter indicating the request is an AJAX request. When the AJAX validation is triggered, a parameter named as this property will be sent together with the other form data to the server. The parameter value is the form ID. The server side can then detect who triggers the AJAX validation and react accordingly. Defaults to 'ajax'.
  • validationUrl: string, the URL that performs the AJAX validations. If not set, it will take the value of CActiveForm::$action.
  • validationDelay: integer, the number of milliseconds that an AJAX validation should be delayed after an input is changed. A value 0 means the validation will be triggered immediately when an input is changed. A value greater than 0 means changing several inputs may only trigger a single validation if they happen fast enough, which may help reduce the server load. Defaults to 200 (0.2 second).
  • validateOnSubmit: boolean, whether to perform AJAX validation when the form is being submitted. If there are any validation errors, the form submission will be stopped. Defaults to false.
  • validateOnChange: boolean, whether to trigger an AJAX validation each time when an input's value is changed. You may want to turn this off if it causes too much performance impact, because each AJAX validation request will submit the data of the whole form. Defaults to true.
  • validateOnType: boolean, whether to trigger an AJAX validation each time when the user presses a key. When setting this property to be true, you should tune up the 'validationDelay' option to avoid triggering too many AJAX validations. Defaults to false.
  • hideErrorMessage: boolean, whether to hide the error message even if there is an error. Defaults to false, which means the error message will show up whenever the input has an error.
  • inputContainer: string, the jQuery selector for the HTML element containing the input field. During the validation process, CActiveForm will set different CSS class for the container element to indicate the state change. If not set, it means the closest 'div' element that contains the input field.
  • errorCssClass: string, the CSS class to be assigned to the container whose associated input has AJAX validation error. Defaults to 'error'.
  • successCssClass: string, the CSS class to be assigned to the container whose associated input passes AJAX validation without any error. Defaults to 'success'.
  • validatingCssClass: string, the CSS class to be assigned to the container whose associated input is currently being validated via AJAX. Defaults to 'validating'.
  • errorMessageCssClass: string, the CSS class assigned to the error messages returned by AJAX validations. Defaults to 'errorMessage'.
  • beforeValidate: function, the function that will be invoked before performing ajax-based validation triggered by form submission action (available only when validateOnSubmit is set true). The expected function signature should be beforeValidate(form) {...}, where 'form' is the jquery representation of the form object. If the return value of this function is NOT true, the validation will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • afterValidate: function, the function that will be invoked after performing ajax-based validation triggered by form submission action (available only when validateOnSubmit is set true). The expected function signature should be afterValidate(form, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error. If the return value of this function is NOT true, the normal form submission will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • beforeValidateAttribute: function, the function that will be invoked before performing ajax-based validation triggered by a single attribute input change. The expected function signature should be beforeValidateAttribute(form, attribute) {...}, where 'form' is the jquery representation of the form object and 'attribute' refers to the js options for the triggering attribute (see CActiveForm::error()). If the return value of this function is NOT true, the validation will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • afterValidateAttribute: function, the function that will be invoked after performing ajax-based validation triggered by a single attribute input change. The expected function signature should be afterValidateAttribute(form, attribute, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'attribute' refers to the js options for the triggering attribute (see CActiveForm::error()); 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.

Some of the above options may be overridden in individual calls of CActiveForm::error(). They include: validationDelay, validateOnChange, validateOnType, hideErrorMessage, inputContainer, errorCssClass, successCssClass, validatingCssClass, beforeValidateAttribute, afterValidateAttribute.

the options to be passed to the javascript validation plugin. The following options are supported:
  • ajaxVar: string, the name of the parameter indicating the request is an AJAX request. When the AJAX validation is triggered, a parameter named as this property will be sent together with the other form data to the server. The parameter value is the form ID. The server side can then detect who triggers the AJAX validation and react accordingly. Defaults to 'ajax'.
  • validationUrl: string, the URL that performs the AJAX validations. If not set, it will take the value of CActiveForm::$action.
  • validationDelay: integer, the number of milliseconds that an AJAX validation should be delayed after an input is changed. A value 0 means the validation will be triggered immediately when an input is changed. A value greater than 0 means changing several inputs may only trigger a single validation if they happen fast enough, which may help reduce the server load. Defaults to 200 (0.2 second).
  • validateOnSubmit: boolean, whether to perform AJAX validation when the form is being submitted. If there are any validation errors, the form submission will be stopped. Defaults to false.
  • validateOnChange: boolean, whether to trigger an AJAX validation each time when an input's value is changed. You may want to turn this off if it causes too much performance impact, because each AJAX validation request will submit the data of the whole form. Defaults to true.
  • validateOnType: boolean, whether to trigger an AJAX validation each time when the user presses a key. When setting this property to be true, you should tune up the 'validationDelay' option to avoid triggering too many AJAX validations. Defaults to false.
  • hideErrorMessage: boolean, whether to hide the error message even if there is an error. Defaults to false, which means the error message will show up whenever the input has an error.
  • inputContainer: string, the jQuery selector for the HTML element containing the input field. During the validation process, CActiveForm will set different CSS class for the container element to indicate the state change. If not set, it means the closest 'div' element that contains the input field.
  • errorCssClass: string, the CSS class to be assigned to the container whose associated input has AJAX validation error. Defaults to 'error'.
  • successCssClass: string, the CSS class to be assigned to the container whose associated input passes AJAX validation without any error. Defaults to 'success'.
  • validatingCssClass: string, the CSS class to be assigned to the container whose associated input is currently being validated via AJAX. Defaults to 'validating'.
  • errorMessageCssClass: string, the CSS class assigned to the error messages returned by AJAX validations. Defaults to 'errorMessage'.
  • beforeValidate: function, the function that will be invoked before performing ajax-based validation triggered by form submission action (available only when validateOnSubmit is set true). The expected function signature should be beforeValidate(form) {...}, where 'form' is the jquery representation of the form object. If the return value of this function is NOT true, the validation will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • afterValidate: function, the function that will be invoked after performing ajax-based validation triggered by form submission action (available only when validateOnSubmit is set true). The expected function signature should be afterValidate(form, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error. If the return value of this function is NOT true, the normal form submission will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • beforeValidateAttribute: function, the function that will be invoked before performing ajax-based validation triggered by a single attribute input change. The expected function signature should be beforeValidateAttribute(form, attribute) {...}, where 'form' is the jquery representation of the form object and 'attribute' refers to the js options for the triggering attribute (see CActiveForm::error()). If the return value of this function is NOT true, the validation will be cancelled.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.
  • afterValidateAttribute: function, the function that will be invoked after performing ajax-based validation triggered by a single attribute input change. The expected function signature should be afterValidateAttribute(form, attribute, data, hasError) {...}, where 'form' is the jquery representation of the form object; 'attribute' refers to the js options for the triggering attribute (see CActiveForm::error()); 'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether there is any validation error.Note that because this option refers to a js function, you should wrap the value with CJavaScriptExpression to prevent it from being encoded as a string. This option has been available since version 1.1.3.

Some of the above options may be overridden in individual calls of CActiveForm::error(). They include: validationDelay, validateOnChange, validateOnType, hideErrorMessage, inputContainer, errorCssClass, successCssClass, validatingCssClass, beforeValidateAttribute, afterValidateAttribute.

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();
    }
    ......
}

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 CValidator::$enableClientValidation and CValidator::clientValidateAttribute()).

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 CValidator::$enableClientValidation and CValidator::clientValidateAttribute()).

Since

1.1.7

See

CActiveForm::error()
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:
  • 'focus'=>array($model,'username') - $model->username input filed
  • 'focus'=>'#'.CHtml::activeId($model,'username') - $model->username input field
  • 'focus'=>'#LoginForm_username' - input field with ID LoginForm_username
  • 'focus'=>'input[type="text"]:first' - first input element of type text
  • 'focus'=>'input:visible:enabled:first' - first visible and enabled input element
  • 'focus'=>'input:text[value=""]:first' - first empty input

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:
  • 'focus'=>array($model,'username') - $model->username input filed
  • 'focus'=>'#'.CHtml::activeId($model,'username') - $model->username input field
  • 'focus'=>'#LoginForm_username' - input field with ID LoginForm_username
  • 'focus'=>'input[type="text"]:first' - first input element of type text
  • 'focus'=>'input:visible:enabled:first' - first visible and enabled input element
  • 'focus'=>'input:text[value=""]:first' - first empty input

Since

1.1.4
protected array $attributes array()
#

the javascript options for model attributes (input ID => options)

the javascript options for model attributes (input ID => options)

Since

1.1.7

See

CActiveForm::error()
protected string $summaryID
#

the ID of the container element for error summary

the ID of the container element for error summary

Since

1.1.7

See

CActiveForm::errorSummary()
Properties inherited from CWidget
$actionPrefix, $skin
Magic properties inherited from CWidget
$controller, $id, $owner, $viewPath
API documentation generated by ApiGen 2.8.0