CHtml

Package system.web.helpers
Inheritance class CHtml
Since 1.0
Source Code framework/web/helpers/CHtml.php
CHtml is a static class that provides a collection of helper methods for creating HTML views.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
afterRequiredLabel string the HTML code to be appended to the required label. CHtml
beforeRequiredLabel string the HTML code to be prepended to the required label. CHtml
closeSingleTags boolean whether to close single tags. CHtml
count integer the counter for generating automatic input field names. CHtml
errorContainerTag string the tag name for the error container tag. CHtml
errorCss string the CSS class for highlighting error inputs. CHtml
errorMessageCss string the CSS class for displaying error messages (see error). CHtml
errorSummaryCss string the CSS class for displaying error summaries (see errorSummary). CHtml
liveEvents Sets the default style for attaching jQuery event handlers. CHtml
renderSpecialAttributesValue boolean whether to render special attributes value. CHtml
requiredCss string the CSS class for required labels. CHtml

Public Methods

Hide inherited methods

MethodDescriptionDefined By
activeCheckBox() Generates a check box for a model attribute. CHtml
activeCheckBoxList() Generates a check box list for a model attribute. CHtml
activeDateField() Generates a date field input for a model attribute. CHtml
activeDropDownList() Generates a drop down list for a model attribute. CHtml
activeEmailField() Generates an email field input for a model attribute. CHtml
activeFileField() Generates a file input for a model attribute. CHtml
activeHiddenField() Generates a hidden input for a model attribute. CHtml
activeId() Generates input field ID for a model attribute. CHtml
activeLabel() Generates a label tag for a model attribute. CHtml
activeLabelEx() Generates a label tag for a model attribute. CHtml
activeListBox() Generates a list box for a model attribute. CHtml
activeName() Generates input field name for a model attribute. CHtml
activeNumberField() Generates a number field input for a model attribute. CHtml
activePasswordField() Generates a password field input for a model attribute. CHtml
activeRadioButton() Generates a radio button for a model attribute. CHtml
activeRadioButtonList() Generates a radio button list for a model attribute. CHtml
activeRangeField() Generates a range field input for a model attribute. CHtml
activeSearchField() Generates a search field input for a model attribute. CHtml
activeTelField() Generates a telephone field input for a model attribute. CHtml
activeTextArea() Generates a text area input for a model attribute. CHtml
activeTextField() Generates a text field input for a model attribute. CHtml
activeTimeField() Generates a time field input for a model attribute. CHtml
activeUrlField() Generates a url field input for a model attribute. CHtml
ajax() Generates the JavaScript that initiates an AJAX request. CHtml
ajaxButton() Generates a push button that can initiate AJAX requests. CHtml
ajaxSubmitButton() Generates a push button that can submit the current form in POST method. CHtml
asset() Generates the URL for the published assets. CHtml
beginForm() Generates an opening form tag. CHtml
button() Generates a button. CHtml
cdata() Encloses the given string within a CDATA tag. CHtml
checkBox() Generates a check box. CHtml
checkBoxList() Generates a check box list. CHtml
closeTag() Generates a close HTML element. CHtml
css() Encloses the given CSS content with a CSS tag. CHtml
cssFile() Links to the specified CSS file. CHtml
dateField() Generates a date field input. CHtml
decode() Decodes special HTML entities back to the corresponding characters. CHtml
emailField() Generates an email field input. CHtml
encode() Encodes special characters into HTML entities. CHtml
encodeArray() Encodes special characters in an array of strings into HTML entities. CHtml
endForm() Generates a closing form tag. CHtml
error() Displays the first validation error for a model attribute. CHtml
errorSummary() Displays a summary of validation errors for one or several models. CHtml
fileField() Generates a file input. CHtml
form() Generates an opening form tag. CHtml
getIdByName() Generates a valid HTML ID based on name. CHtml
hiddenField() Generates a hidden input. CHtml
htmlButton() Generates a button using HTML button tag. CHtml
image() Generates an image tag. CHtml
imageButton() Generates an image submit button. CHtml
label() Generates a label tag. CHtml
linkButton() Generates a link submit button. CHtml
linkTag() Generates a link tag that can be inserted in the head section of HTML page. CHtml
listBox() Generates a list box. CHtml
listData() Generates the data suitable for list-based HTML elements. CHtml
listOptions() Generates the list options. CHtml
mailto() Generates a mailto link. CHtml
metaTag() Generates a meta tag that can be inserted in the head section of HTML page. CHtml
modelName() Generates HTML name for given model. CHtml
normalizeUrl() Normalizes the input parameter to be a valid URL. CHtml
numberField() Generates a number field input. CHtml
openTag() Generates an open HTML element. CHtml
pageStateField() Generates a hidden field for storing persistent page states. CHtml
passwordField() Generates a password field input. CHtml
radioButton() Generates a radio button. CHtml
radioButtonList() Generates a radio button list. CHtml
rangeField() Generates a range field input. CHtml
refresh() Registers a 'refresh' meta tag. CHtml
renderAttributes() Renders the HTML tag attributes. CHtml
resetButton() Generates a reset button. CHtml
resolveName() Generates input name for a model attribute. CHtml
resolveNameID() Generates input name and ID for a model attribute. CHtml
resolveValue() Evaluates the attribute value of the model. CHtml
script() Encloses the given JavaScript within a script tag. CHtml
scriptFile() Includes a JavaScript file. CHtml
setModelNameConverter() Set generator used in the CHtml::modelName() method. You can use the `null` value to restore default CHtml
statefulForm() Generates a stateful form tag. CHtml
submitButton() Generates a submit button. CHtml
tag() Generates an HTML element. CHtml
telField() Generates a telephone field input. CHtml
textArea() Generates a text area input. CHtml
textField() Generates a text field input. CHtml
timeField() Generates a time field input. CHtml
urlField() Generates a URL field input. CHtml
value() Evaluates the value of the specified attribute for the given model. CHtml

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
activeInputField() Generates an input HTML tag for a model attribute. CHtml
addErrorCss() Appends errorCss to the 'class' attribute. CHtml
clientChange() Generates the JavaScript with the specified client changes. CHtml
inputField() Generates an input HTML tag. CHtml

Property Details

afterRequiredLabel property
public static string $afterRequiredLabel;

the HTML code to be appended to the required label.

See Also

beforeRequiredLabel property
public static string $beforeRequiredLabel;

the HTML code to be prepended to the required label.

See Also

closeSingleTags property (available since v1.1.13)
public static boolean $closeSingleTags;

whether to close single tags. Defaults to true. Can be set to false for HTML5.

count property
public static integer $count;

the counter for generating automatic input field names.

errorContainerTag property (available since v1.1.13)
public static string $errorContainerTag;

the tag name for the error container tag. Defaults to 'div'.

errorCss property
public static string $errorCss;

the CSS class for highlighting error inputs. Form inputs will be appended with this CSS class if they have input errors.

errorMessageCss property
public static string $errorMessageCss;

the CSS class for displaying error messages (see error).

errorSummaryCss property
public static string $errorSummaryCss;

the CSS class for displaying error summaries (see errorSummary).

liveEvents property (available since v1.1.9)
public static $liveEvents;

Sets the default style for attaching jQuery event handlers.

If set to true (default), event handlers are delegated. Event handlers are attached to the document body and can process events from descendant elements that are added to the document at a later time.

If set to false, event handlers are directly bound. Event handlers are attached directly to the DOM element, that must already exist on the page. Elements injected into the page at a later time will not be processed.

You can override this setting for a particular element by setting the htmlOptions delegate attribute (see clientChange).

For more information about attaching jQuery event handler see http://api.jquery.com/on/

See Also

renderSpecialAttributesValue property (available since v1.1.13)
public static boolean $renderSpecialAttributesValue;

whether to render special attributes value. Defaults to true. Can be set to false for HTML5.

requiredCss property
public static string $requiredCss;

the CSS class for required labels. Defaults to 'required'.

See Also

Method Details

activeCheckBox() method
public static string activeCheckBox(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.) A special option named 'uncheckValue' is available that can be used to specify the value returned when the checkbox is not checked. By default, this value is '0'. Internally, a hidden field is rendered so that when the checkbox is not checked, we can still obtain the posted uncheck value. If 'uncheckValue' is set as NULL, the hidden field will not be rendered.
{return} string the generated check box
Source Code: framework/web/helpers/CHtml.php#1738 (show)
public static function activeCheckBox($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    if(!isset(
$htmlOptions['value']))
        
$htmlOptions['value']=1;
    if(!isset(
$htmlOptions['checked']) && self::resolveValue($model,$attribute)==$htmlOptions['value'])
        
$htmlOptions['checked']='checked';
    
self::clientChange('click',$htmlOptions);

    if(
array_key_exists('uncheckValue',$htmlOptions))
    {
        
$uncheck=$htmlOptions['uncheckValue'];
        unset(
$htmlOptions['uncheckValue']);
    }
    else
        
$uncheck='0';

    
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
    
$hidden=$uncheck!==null self::hiddenField($htmlOptions['name'],$uncheck,$hiddenOptions) : '';

    return 
$hidden self::activeInputField('checkbox',$model,$attribute,$htmlOptions);
}

Generates a check box for a model attribute. The attribute is assumed to take either true or false value. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeCheckBoxList() method
public static string activeCheckBoxList(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. Note, the values will be automatically HTML-encoded, while the labels will not.
$htmlOptions array additional HTML options. The options will be applied to each checkbox input. The following special options are recognized:
  • template: string, specifies how each checkbox is rendered. Defaults to "{input} {label}", where "{input}" will be replaced by the generated check box input tag while "{label}" will be replaced by the corresponding check box label.
  • separator: string, specifies the string that separates the generated check boxes.
  • checkAll: string, specifies the label for the "check all" checkbox. If this option is specified, a 'check all' checkbox will be displayed. Clicking on this checkbox will cause all checkboxes checked or unchecked.
  • checkAllLast: boolean, specifies whether the 'check all' checkbox should be displayed at the end of the checkbox list. If this option is not set (default) or is false, the 'check all' checkbox will be displayed at the beginning of the checkbox list.
  • encode: boolean, specifies whether to encode HTML-encode tag attributes and values. Defaults to true.
Since 1.1.7, a special option named 'uncheckValue' is available. It can be used to set the value that will be returned when the checkbox is not checked. By default, this value is ''. Internally, a hidden field is rendered so when the checkbox is not checked, we can still obtain the value. If 'uncheckValue' is set to NULL, there will be no hidden field rendered.
{return} string the generated check box list
Source Code: framework/web/helpers/CHtml.php#1902 (show)
public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
$selection=self::resolveValue($model,$attribute);
    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);
    
$name=$htmlOptions['name'];
    unset(
$htmlOptions['name']);

    if(
array_key_exists('uncheckValue',$htmlOptions))
    {
        
$uncheck=$htmlOptions['uncheckValue'];
        unset(
$htmlOptions['uncheckValue']);
    }
    else
        
$uncheck='';

    
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
    
$hidden=$uncheck!==null self::hiddenField($name,$uncheck,$hiddenOptions) : '';

    return 
$hidden self::checkBoxList($name,$selection,$data,$htmlOptions);
}

Generates a check box list for a model attribute. The model attribute value is used as the selection. If the attribute has input error, the input field's CSS class will be appended with errorCss. Note that a check box list allows multiple selection, like listBox. As a result, the corresponding POST value is an array. In case no selection is made, the corresponding POST value is an empty string.

See Also

activeDateField() method (available since v1.1.11)
public static string activeDateField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1550 (show)
public static function activeDateField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    return 
self::activeInputField('date',$model,$attribute,$htmlOptions);
}

Generates a date field input for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeDropDownList() method
public static string activeDropDownList(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) You may use listData to generate this data. Please refer to listOptions on how this data is used to generate the list options. Note, the values and labels will be automatically HTML-encoded by this method.
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are recognized. See clientChange and tag for more details. In addition, the following options are also supported:
  • encode: boolean, specifies whether to encode the values. Defaults to true.
  • prompt: string, specifies the prompt text shown as the first list option. Its value is empty. Note, the prompt text will NOT be HTML-encoded.
  • empty: string, specifies the text corresponding to empty selection. Its value is empty. The 'empty' option can also be an array of value-label pairs. Each pair will be used to render a list option at the beginning. Note, the text label will NOT be HTML-encoded.
  • options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
        array(
            'value1'=>array('disabled'=>true,'label'=>'value 1'),
            'value2'=>array('label'=>'value 2'),
        );
    
Since 1.1.13, a special option named 'unselectValue' is available. It can be used to set the value that will be returned when no option is selected in multiple mode. When set, a hidden field is rendered so that if no option is selected in multiple mode, we can still obtain the posted unselect value. If 'unselectValue' is not set or set to NULL, the hidden field will not be rendered.
{return} string the generated drop down list
Source Code: framework/web/helpers/CHtml.php#1799 (show)
public static function activeDropDownList($model,$attribute,$data,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
$selection=self::resolveValue($model,$attribute);
    
$options="\n".self::listOptions($selection,$data,$htmlOptions);
    
self::clientChange('change',$htmlOptions);

    if(
$model->hasErrors($attribute))
        
self::addErrorCss($htmlOptions);

    
$hidden='';
    if(!empty(
$htmlOptions['multiple']))
    {
        if(
substr($htmlOptions['name'],-2)!=='[]')
            
$htmlOptions['name'].='[]';

        if(isset(
$htmlOptions['unselectValue']))
        {
            
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
            
$hidden=self::hiddenField(substr($htmlOptions['name'],0,-2),$htmlOptions['unselectValue'],$hiddenOptions);
            unset(
$htmlOptions['unselectValue']);
        }
    }
    return 
$hidden self::tag('select',$htmlOptions,$options);
}

Generates a drop down list for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeEmailField() method (available since v1.1.11)
public static string activeEmailField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes. Besides normal HTML attributes, a few special attributes are also recognized (see clientChange and tag for more details.)
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1490 (show)
public static function activeEmailField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
self::clientChange('change',$htmlOptions);
    return 
self::activeInputField('email',$model,$attribute,$htmlOptions);
}

Generates an email field input for a model attribute. If the attribute has input error, the input field's CSS class will be appended with errorCss.

activeFileField() method
public static string activeFileField(CModel $model, string $attribute, array $htmlOptions=array ( ))
$model CModel the data model
$attribute string the attribute
$htmlOptions array additional HTML attributes (see tag).
{return} string the generated input field
Source Code: framework/web/helpers/CHtml.php#1669 (show)
public static function activeFileField($model,$attribute,$htmlOptions=array())
{
    
self::resolveNameID($model,$attribute,$htmlOptions);
    
// add a hidden field so that if a model only has a file field, we can
    // still use isset($_POST[$modelClass]) to detect if the input is submitted
    
$hiddenOptions=isset($htmlOptions['id']) ? array('id'=>self::ID_PREFIX.$htmlOptions['id']) : array('id'=>false);
    return 
self::hiddenField($htmlOptions['name'],'',$hiddenOptions)
        . 
self::activeInputField('file',$model,$attribute,$htmlOptions);
}

Generates a file input for a model attribute. Note, you have to set the enclosing form's 'enctype' attribute to be 'multipart/form-data'. After the form is submitted, the uploaded file information can be obtained via $_FILES (see PHP documentation).

best hosting
X