Package | system.web.widgets |
---|---|
Inheritance | class CActiveForm » CWidget » CBaseController » CComponent |
Subclasses | CCodeForm |
Since | 1.1.1 |
Source Code | framework/web/widgets/CActiveForm.php |
<?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(); ?>
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(); } }
performAjaxValidation
method and its invocation.Property | Type | Description | Defined By |
---|---|---|---|
action | mixed | the form action URL (see CHtml::normalizeUrl for details about this parameter). | CActiveForm |
actionPrefix | string | the prefix to the IDs of the actions. | CWidget |
clientOptions | array | the options to be passed to the javascript validation plugin. | CActiveForm |
controller | CController | Returns the controller that this widget belongs to. | CWidget |
enableAjaxValidation | boolean | whether to enable data validation via AJAX. | CActiveForm |
enableClientValidation | boolean | whether to enable client-side data validation. | CActiveForm |
errorMessageCssClass | string | the CSS class name for error messages. | CActiveForm |
focus | mixed | form element to get initial input focus on page load. | CActiveForm |
htmlOptions | array | additional HTML attributes that should be rendered for the form tag. | CActiveForm |
id | string | Returns the ID of the widget or generates a new one if requested. | CWidget |
method | string | the form submission method. | CActiveForm |
owner | CBaseController | Returns the owner/creator of this widget. | CWidget |
skin | mixed | the name of the skin to be used by this widget. | CWidget |
stateful | boolean | whether to generate a stateful form (See CHtml::statefulForm). | CActiveForm |
viewPath | string | Returns the directory containing the view files for this widget. | CWidget |
Property | Type | Description | Defined By |
---|---|---|---|
attributes | array | the javascript options for model attributes (input ID => options) | CActiveForm |
summaryID | string | the ID of the container element for error summary | CActiveForm |
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | CComponent |
__construct() | Constructor. | CWidget |
__get() | Returns a property value, an event handler list or a behavior based on its name. | CComponent |
__isset() | Checks if a property value is null. | CComponent |
__set() | Sets value of a component property. | CComponent |
__unset() | Sets a component property to be null. | CComponent |
actions() | Returns a list of actions that are used by this widget. | CWidget |
asa() | Returns the named behavior object. | CComponent |
attachBehavior() | Attaches a behavior to this component. | CComponent |
attachBehaviors() | Attaches a list of behaviors to the component. | CComponent |
attachEventHandler() | Attaches an event handler to an event. | CComponent |
beginCache() | Begins fragment caching. | CBaseController |
beginClip() | Begins recording a clip. | CBaseController |
beginContent() | Begins the rendering of content that is to be decorated by the specified view. | CBaseController |
beginWidget() | Creates a widget and executes it. | CBaseController |
canGetProperty() | Determines whether a property can be read. | CComponent |
canSetProperty() | Determines whether a property can be set. | CComponent |
checkBox() | Renders a checkbox for a model attribute. | CActiveForm |
checkBoxList() | Renders a checkbox list for a model attribute. | CActiveForm |
colorField() | Renders a color picker field for a model attribute. | CActiveForm |
createWidget() | Creates a widget and initializes it. | CBaseController |
dateField() | Renders a date field for a model attribute. | CActiveForm |
dateTimeField() | Renders a datetime field for a model attribute. | CActiveForm |
dateTimeLocalField() | Renders a local datetime field for a model attribute. | CActiveForm |
detachBehavior() | Detaches a behavior from the component. | CComponent |
detachBehaviors() | Detaches all behaviors from the component. | CComponent |
detachEventHandler() | Detaches an existing event handler. | CComponent |
disableBehavior() | Disables an attached behavior. | CComponent |
disableBehaviors() | Disables all behaviors attached to this component. | CComponent |
dropDownList() | Renders a dropdown list for a model attribute. | CActiveForm |
emailField() | Renders an email field for a model attribute. | CActiveForm |
enableBehavior() | Enables an attached behavior. | CComponent |
enableBehaviors() | Enables all behaviors attached to this component. | CComponent |
endCache() | Ends fragment caching. | CBaseController |
endClip() | Ends recording a clip. | CBaseController |
endContent() | Ends the rendering of content. | CBaseController |
endWidget() | Ends the execution of the named widget. | CBaseController |
error() | Displays the first validation error for a model attribute. | CActiveForm |
errorSummary() | Displays a summary of validation errors for one or several models. | CActiveForm |
evaluateExpression() | Evaluates a PHP expression or callback under the context of this component. | CComponent |
fileField() | Renders a file field for a model attribute. | CActiveForm |
getController() | Returns the controller that this widget belongs to. | CWidget |
getEventHandlers() | Returns the list of attached event handlers for an event. | CComponent |
getId() | Returns the ID of the widget or generates a new one if requested. | CWidget |
getOwner() | Returns the owner/creator of this widget. | CWidget |
getViewFile() | Looks for the view script file according to the view name. | CWidget |
getViewPath() | Returns the directory containing the view files for this widget. | CWidget |
hasEvent() | Determines whether an event is defined. | CComponent |
hasEventHandler() | Checks whether the named event has attached handlers. | CComponent |
hasProperty() | Determines whether a property is defined. | CComponent |
init() | Initializes the widget. | CActiveForm |
label() | Renders an HTML label for a model attribute. | CActiveForm |
labelEx() | Renders an HTML label for a model attribute. | CActiveForm |
listBox() | Renders a list box for a model attribute. | CActiveForm |
numberField() | Renders a number field for a model attribute. | CActiveForm |
passwordField() | Renders a password field for a model attribute. | CActiveForm |
radioButton() | Renders a radio button for a model attribute. | CActiveForm |
radioButtonList() | Renders a radio button list for a model attribute. | CActiveForm |
raiseEvent() | Raises an event. | CComponent |
rangeField() | Generates a range field for a model attribute. | CActiveForm |
render() | Renders a view. | CWidget |
renderFile() | Renders a view file. | CBaseController |
renderInternal() | Renders a view file. | CBaseController |
run() | Runs the widget. | CActiveForm |
searchField() | Renders a search field for a model attribute. | CActiveForm |
setId() | Sets the ID of the widget. | CWidget |
telField() | Renders a tel field for a model attribute. | CActiveForm |
textArea() | Renders a text area for a model attribute. | CActiveForm |
textField() | Renders a text field for a model attribute. | CActiveForm |
timeField() | Renders a time field for a model attribute. | CActiveForm |
urlField() | Renders a url field for a model attribute. | CActiveForm |
validate() | Validates one or several models and returns the results in JSON format. | CActiveForm |
validateTabular() | Validates an array of model instances and returns the results in JSON format. | CActiveForm |
weekField() | Renders a week field for a model attribute. | CActiveForm |
widget() | Creates a widget and executes it. | CBaseController |
the form action URL (see CHtml::normalizeUrl for details about this parameter). If not set, the current page URL is used.
the javascript options for model attributes (input ID => options)
the options to be passed to the javascript validation plugin. The following options are supported:
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.
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.
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 error).
If the return value of this function is NOT true, the validation will be cancelled.
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 error);
'data' is the JSON response from the server-side validation; 'hasError' is a boolean value indicating whether
there is any validation error.
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 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).
the CSS class name for error messages. Since 1.1.14 this defaults to 'errorMessage' defined in CHtml::$errorMessageCss. Individual error call may override this value by specifying the 'class' HTML option.
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:
additional HTML attributes that should be rendered for the form tag.
the form submission method. This should be either 'post' or 'get'. Defaults to 'post'.
whether to generate a stateful form (See CHtml::statefulForm). Defaults to false.
the ID of the container element for error summary
public string checkBox(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated check box |
public function checkBox($model,$attribute,$htmlOptions=array())
{
return CHtml::activeCheckBox($model,$attribute,$htmlOptions);
}
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.
public string checkBoxList(CModel $model, string $attribute, array $data, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$data | array | value-label pairs used to generate the check box list. |
$htmlOptions | array | addtional HTML options. |
{return} | string | the generated check box list |
public function checkBoxList($model,$attribute,$data,$htmlOptions=array())
{
return CHtml::activeCheckBoxList($model,$attribute,$data,$htmlOptions);
}
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.
public string colorField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function colorField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeColorField($model,$attribute,$htmlOptions);
}
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.
public string dateField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function dateField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeDateField($model,$attribute,$htmlOptions);
}
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.
public string dateTimeField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function dateTimeField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeDateTimeField($model,$attribute,$htmlOptions);
}
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.
public string dateTimeLocalField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function dateTimeLocalField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeDateTimeLocalField($model,$attribute,$htmlOptions);
}
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.
public string dropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$data | array | data for generating the list options (value=>display) |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated drop down list |
public function dropDownList($model,$attribute,$data,$htmlOptions=array())
{
return CHtml::activeDropDownList($model,$attribute,$data,$htmlOptions);
}
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.
public string emailField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function emailField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeEmailField($model,$attribute,$htmlOptions);
}
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.
public string error(CModel $model, string $attribute, array $htmlOptions=array (
), boolean $enableAjaxValidation=true, boolean $enableClientValidation=true)
| ||
$model | CModel | the data model |
$attribute | string | the attribute name |
$htmlOptions | array | 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:
Example: <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> 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:
|
$enableAjaxValidation | boolean | whether to enable AJAX validation for the specified attribute. Note that in order to enable AJAX validation, both enableAjaxValidation and this parameter must be true. |
$enableClientValidation | boolean | whether to enable client-side validation for the specified attribute. Note that in order to enable client-side validation, both enableClientValidation and this parameter must be true. This parameter has been available since version 1.1.7. |
{return} | string | the validation result (error display or success message). |
public function error($model,$attribute,$htmlOptions=array(),$enableAjaxValidation=true,$enableClientValidation=true)
{
if(!$this->enableAjaxValidation)
$enableAjaxValidation=false;
if(!$this->enableClientValidation)
$enableClientValidation=false;
if(!isset($htmlOptions['class']))
$htmlOptions['class']=$this->errorMessageCssClass;
if(!$enableAjaxValidation && !$enableClientValidation)
return CHtml::error($model,$attribute,$htmlOptions);
$id=CHtml::activeId($model,$attribute);
$inputID=isset($htmlOptions['inputID']) ? $htmlOptions['inputID'] : $id;
unset($htmlOptions['inputID']);
if(!isset($htmlOptions['id']))
$htmlOptions['id']=$inputID.'_em_';
$option=array(
'id'=>$id,
'inputID'=>$inputID,
'errorID'=>$htmlOptions['id'],
'model'=>get_class($model),
'name'=>$attribute,
'enableAjaxValidation'=>$enableAjaxValidation,
);
$optionNames=array(
'validationDelay',
'validateOnChange',
'validateOnType',
'hideErrorMessage',
'inputContainer',
'errorCssClass',
'successCssClass',
'validatingCssClass',
'beforeValidateAttribute',
'afterValidateAttribute',
);
foreach($optionNames as $name)
{
if(isset($htmlOptions[$name]))
{
$option[$name]=$htmlOptions[$name];
unset($htmlOptions[$name]);
}
}
if($model instanceof CActiveRecord && !$model->isNewRecord)
$option['status']=1;
if($enableClientValidation)
{
$validators=isset($htmlOptions['clientValidation']) ? array($htmlOptions['clientValidation']) : array();
unset($htmlOptions['clientValidation']);
$attributeName = $attribute;
if(($pos=strrpos($attribute,']'))!==false && $pos!==strlen($attribute)-1) // e.g. [a]name
{
$attributeName=substr($attribute,$pos+1);
}
foreach($model->getValidators($attributeName) as $validator)
{
if($validator->enableClientValidation)
{
if(($js=$validator->clientValidateAttribute($model,$attributeName))!='')
$validators[]=$js;
}
}
if($validators!==array())
$option['clientValidation']=new CJavaScriptExpression("function(value, messages, attribute) {\n".implode("\n",$validators)."\n}");
}
if(empty($option['hideErrorMessage']) && empty($this->clientOptions['hideErrorMessage']))
$html=CHtml::error($model,$attribute,$htmlOptions);
else
$html='';
if($html==='')
{
if(isset($htmlOptions['style']))
$htmlOptions['style']=rtrim($htmlOptions['style'],';').';display:none';
else
$htmlOptions['style']='display:none';
$html=CHtml::tag(CHtml::$errorContainerTag,$htmlOptions,'');
}
$this->attributes[$inputID]=$option;
return $html;
}
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.
public string errorSummary(mixed $models, string $header=NULL, string $footer=NULL, array $htmlOptions=array (
))
| ||
$models | mixed | the models whose input errors are to be displayed. This can be either a single model or an array of models. |
$header | string | a piece of HTML code that appears in front of the errors |
$footer | string | a piece of HTML code that appears at the end of the errors |
$htmlOptions | array | additional HTML attributes to be rendered in the container div tag. |
{return} | string | the error summary. Empty if no errors are found. |
public function errorSummary($models,$header=null,$footer=null,$htmlOptions=array())
{
if(!$this->enableAjaxValidation && !$this->enableClientValidation)
return CHtml::errorSummary($models,$header,$footer,$htmlOptions);
if(!isset($htmlOptions['id']))
$htmlOptions['id']=$this->id.'_es_';
$html=CHtml::errorSummary($models,$header,$footer,$htmlOptions);
if($html==='')
{
if($header===null)
$header='<p>'.Yii::t('yii','Please fix the following input errors:').'</p>';
if(!isset($htmlOptions['class']))
$htmlOptions['class']=CHtml::$errorSummaryCss;
$htmlOptions['style']=isset($htmlOptions['style']) ? rtrim($htmlOptions['style'],';').';display:none' : 'display:none';
$html=CHtml::tag('div',$htmlOptions,$header."\n<ul><li>dummy</li></ul>".$footer);
}
$this->summaryID=$htmlOptions['id'];
foreach(is_array($models) ? $models : array($models) as $model)
foreach($model->getSafeAttributeNames() as $attribute)
$this->_summaryAttributes[]=CHtml::activeId($model,$attribute);
return $html;
}
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.
public string fileField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes |
{return} | string | the generated input field |
public function fileField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeFileField($model,$attribute,$htmlOptions);
}
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.
public string hiddenField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function hiddenField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeHiddenField($model,$attribute,$htmlOptions);
}
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.
public void init()
|
public function init()
{
if(!isset($this->htmlOptions['id']))
$this->htmlOptions['id']=$this->id;
else
$this->id=$this->htmlOptions['id'];
if($this->stateful)
echo CHtml::statefulForm($this->action, $this->method, $this->htmlOptions);
else
echo CHtml::beginForm($this->action, $this->method, $this->htmlOptions);
if($this->errorMessageCssClass===null)
$this->errorMessageCssClass=CHtml::$errorMessageCss;
}
Initializes the widget. This renders the form open tag.
public string label(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated label tag |
public function label($model,$attribute,$htmlOptions=array())
{
return CHtml::activeLabel($model,$attribute,$htmlOptions);
}
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.
public string labelEx(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated label tag |
public function labelEx($model,$attribute,$htmlOptions=array())
{
return CHtml::activeLabelEx($model,$attribute,$htmlOptions);
}
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.
public string listBox(CModel $model, string $attribute, array $data, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$data | array | data for generating the list options (value=>display) |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated list box |
public function listBox($model,$attribute,$data,$htmlOptions=array())
{
return CHtml::activeListBox($model,$attribute,$data,$htmlOptions);
}
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.
public string numberField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function numberField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeNumberField($model,$attribute,$htmlOptions);
}
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.
public string passwordField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function passwordField($model,$attribute,$htmlOptions=array())
{
return CHtml::activePasswordField($model,$attribute,$htmlOptions);
}
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.
public string radioButton(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated radio button |
public function radioButton($model,$attribute,$htmlOptions=array())
{
return CHtml::activeRadioButton($model,$attribute,$htmlOptions);
}
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.
public string radioButtonList(CModel $model, string $attribute, array $data, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$data | array | value-label pairs used to generate the radio button list. |
$htmlOptions | array | addtional HTML options. |
{return} | string | the generated radio button list |
public function radioButtonList($model,$attribute,$data,$htmlOptions=array())
{
return CHtml::activeRadioButtonList($model,$attribute,$data,$htmlOptions);
}
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.
public string rangeField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function rangeField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeRangeField($model,$attribute,$htmlOptions);
}
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.
public void run()
|
public function run()
{
if(is_array($this->focus))
$this->focus="#".CHtml::activeId($this->focus[0],$this->focus[1]);
echo CHtml::endForm();
$cs=Yii::app()->clientScript;
if(!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))
{
if($this->focus!==null)
{
$cs->registerCoreScript('jquery');
$cs->registerScript('CActiveForm#focus',"
if(!window.location.hash)
jQuery('".$this->focus."').focus();
");
}
return;
}
$options=$this->clientOptions;
if(isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))
$options['validationUrl']=CHtml::normalizeUrl($this->clientOptions['validationUrl']);
foreach($this->_summaryAttributes as $attribute)
$this->attributes[$attribute]['summary']=true;
$options['attributes']=array_values($this->attributes);
if($this->summaryID!==null)
$options['summaryID']=$this->summaryID;
if($this->focus!==null)
$options['focus']=$this->focus;
if(!empty(CHtml::$errorCss))
$options['errorCss']=CHtml::$errorCss;
$options=CJavaScript::encode($options);
$cs->registerCoreScript('yiiactiveform');
$id=$this->id;
$cs->registerScript(__CLASS__.'#'.$id,"jQuery('#$id').yiiactiveform($options);");
}
Runs the widget. This registers the necessary javascript code and renders the form close tag.
public string searchField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function searchField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeSearchField($model,$attribute,$htmlOptions);
}
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.
public string telField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function telField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeTelField($model,$attribute,$htmlOptions);
}
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.
public string textArea(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated text area |
public function textArea($model,$attribute,$htmlOptions=array())
{
return CHtml::activeTextArea($model,$attribute,$htmlOptions);
}
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.
public string textField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function textField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeTextField($model,$attribute,$htmlOptions);
}
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.
public string timeField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function timeField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeTimeField($model,$attribute,$htmlOptions);
}
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.
public string urlField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function urlField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeUrlField($model,$attribute,$htmlOptions);
}
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.
public static string validate(mixed $models, array $attributes=NULL, boolean $loadInput=true)
| ||
$models | mixed | a single model instance or an array of models. |
$attributes | array | 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 | whether to load the data from $_POST array in this method.
If this is true, the model will be populated from $_POST[ModelClass] . |
{return} | string | the JSON representation of the validation error messages. |
public static function validate($models, $attributes=null, $loadInput=true)
{
$result=array();
if(!is_array($models))
$models=array($models);
foreach($models as $model)
{
$modelName=CHtml::modelName($model);
if($loadInput && isset($_POST[$modelName]))
$model->attributes=$_POST[$modelName];
$model->validate($attributes);
foreach($model->getErrors() as $attribute=>$errors)
$result[CHtml::activeId($model,$attribute)]=$errors;
}
return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result);
}
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.
public static string validateTabular(mixed $models, array $attributes=NULL, boolean $loadInput=true)
| ||
$models | mixed | an array of model instances. |
$attributes | array | 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 | whether to load the data from $_POST array in this method.
If this is true, the model will be populated from $_POST[ModelClass][$i] . |
{return} | string | the JSON representation of the validation error messages. |
public static function validateTabular($models, $attributes=null, $loadInput=true)
{
$result=array();
if(!is_array($models))
$models=array($models);
foreach($models as $i=>$model)
{
$modelName=CHtml::modelName($model);
if($loadInput && isset($_POST[$modelName][$i]))
$model->attributes=$_POST[$modelName][$i];
$model->validate($attributes);
foreach($model->getErrors() as $attribute=>$errors)
$result[CHtml::activeId($model,'['.$i.']'.$attribute)]=$errors;
}
return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result);
}
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.
public string weekField(CModel $model, string $attribute, array $htmlOptions=array (
))
| ||
$model | CModel | the data model |
$attribute | string | the attribute |
$htmlOptions | array | additional HTML attributes. |
{return} | string | the generated input field |
public function weekField($model,$attribute,$htmlOptions=array())
{
return CHtml::activeWeekField($model,$attribute,$htmlOptions);
}
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.
Ajax/client-side validation not working without calling $form->error()
If you want to use ajax validation or client side validation, be sure to call..
$form->error($model, 'field')
..in the view file for each field that requires live validation.
In my case I was only calling errorSummary() and couldn't figure out why no validation was being performed.
AJAX validation requiring proper ID
Common mistake is to copy code presented in the begining of this page without modifing ID of a form. If you want to use AJAX validation, be sure that this line in your controller:
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
and this, in your view:
<?php $form = $this->beginWidget('CActiveForm', array( 'id'=>'user-form',
are using exactly the same ID. If not, you may observe that AJAX validation request is being sent, and reply is properly returned, but nothing on your page changes.
Signup or Login in order to comment.