Tuesday, 23 February 2016

Yii CHtml Controls and Examples

    CHtml::activeCheckBox()

    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.
    Syntax:
    public static string activeCheckBox(CModel $model, string $attribute, array $htmlOptions=array ( ))
    Example:
    $model = Post::model()->findByPk(1);
    echo CHtml::activeCheckBox($model, 'status', array('1'=>'Draft','2'=>'Publish'));

    CHtml::activeCheckBoxList()

    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.
    Syntax:
    public static string activeCheckBoxList(CModel $model, string $attribute, array $data, array $htmlOptions=array ( ))
    Example:
    /**
    Suppose that:
    1. category_id is an attribte of Post model.
    2. id, title are attribtes of Category model.
    */
     
    $model = Post::model()->findByPk(1);
    echo CHtml::activeCheckBoxList($model, 'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'title'));

    CHtml::button()

    Create a html button
    Syntax:
    public static string button(string $label='button', array $htmlOptions=array ( ))
    Example:
    echo CHtml::button('Submit', array('submit' => array('post/create')));

    CHtml::textField()

    Create a html textfield input.
    Syntax:
    public static function textField($name,$value='',$htmlOptions=array())
    Example:
    CHtml::textField('Text name', 'This is default value');

    CHtml::dropDownList()

    Generates a drop down list.
    Syntax:
    public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
    Example:
    $selected = 1;
    echo CHtml::dropDownList('status', $selected, 
                  array('1' => 'Draft', '2' => 'Publish');

    CHtml::listOptions()

    Generates the list options.
    Syntax:
    public static string listOptions(mixed $selection, array $listData, array &$htmlOptions)
    Example:
    $selection = 'M'; // or 'F'
    $htmlOptions = array('id' => 'option_id');
    echo CHtml::listOptions($selection, 
                  array('M' => 'Male', 'F' => 'Female'),
                  $htmlOptions
                  );

    CHtml::listData()

    Generates the data suitable for list-based HTML elements. The generated data can be used in dropDownList, listBox, checkBoxList, radioButtonList, and their active-versions (such as activeDropDownList)
    Syntax:
    public static array listData(array $models, mixed $valueField, mixed $textField, mixed $groupField='')
    Example:
    $models = categories::model()->findAll();
    //format models resulting using listData     
    $category_list = CHtml::listData($models,'category_id', 'category_name');    
    print_r($category_list);

    CHtml::cdata()

    Encloses the given string within a CDATA tag.
    Syntax:
    public static string cdata(string $text)
    Example:
    <![CDATA[This is CData content]]>

    CHtml::label()

    Generates a label tag.
    Syntax:
    public static string label(string $label, string $for, array $htmlOptions=array ( ))
    Example:
    echo CHtml::label('Click here','checkbox-id');
    echo CHtml::checkbox('checkbox_name',true,array('id'=>'checkbox_id','class'=>'checkbox_class'));

    CHtml::checkBoxList()

    Generates a check box list. A check box list allows multiple selection, like listBox. As a result, the corresponding POST value is an array.
    Syntax:
    public static string checkBoxList(string $name, mixed $select, array $data, array $htmlOptions=array ( ))
    Example:
    echo "<strong>".CHtml::label('Simple Poll','checkbox-list-id') . "</strong><br>";
     
    echo CHtml::checkBoxList('checkbox_list_name', 'codexamples', array(
        'codexamples' => 'Codexamples.com',
        'google' => 'Google.com',
        'wikipedia' => 'Wikipedia.com',
        'youtube' =>'Youtube.com'
        ), 
    array('id'=>'checkbox-list-id','class'=>'checkboxlist'));

    CHtml::checkBox

    Generates a check box.
    Syntax:
    public static string checkBox(string $name, boolean $checked=false, array $htmlOptions=array ( ))
    Example:
    echo CHtml::label('Click here','checkbox-id');
    echo CHtml::checkbox('checkbox_name',true,array('id'=>'checkbox_id','class'=>'checkbox_class'));

    CHtml::link()

    Generating a link tag.
    Syntax:
    public static string link(string $text, mixed $url='#', array $htmlOptions=array ( ))
    Example:
    CHtml::link('Post Link',array('post/index'));

    CHtml::cssFile()

    Links to the specified CSS file.
    Syntax:
    public static string cssFile(string $url, string $media='')
    Example:
    $url = '/css/style.css';
    echo CHtml::cssFile($url,'screen');

    CHtml::dateField()

    Generates a date field input.
    Syntax:
    public static string dateField(string $name, string $value='', array $htmlOptions=array ( ))
    Example:
    echo CHtml::dateField('date_name',date("Y-m-d"),array('id'=>'date_id'));

    CHtml::emailField()

    Generates an email field input.
    Syntax:
    public static string emailField(string $name, string $value='', array $htmlOptions=array ( ))
    Example:
    echo CHtml::emailField('email_name','default@email.com',array('id'=>'date_id','placeholder'=>'Email field'));

    CHtml::error()

    Displays the first validation error for a model attribute.
    Syntax:
    public static string error(CModel $model, string $attribute, array $htmlOptions=array ( ))
    Example:
    echo $form->error($model,'attribute_name');

    CHtml::encodeArray()

    Encodes special characters in an array of strings into HTML entities. Both the array keys and values will be encoded if needed. If a value is an array, this method will also encode it recursively. The application charset will be used for encoding.
    Syntax:
    public static array encodeArray(array $data)
    Example:
    $array = array(
        'cat1'=>'<p>This is content of category number 1</p>',
        'cat2'=>'<p>This is content of category number 3</p>',
        );
     
    $encodeArray = CHtml::encodeArray($array);
    print_r($encodeArray);

    CHtml::fileField()

    Generates a file input. 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[$name] (see PHP documentation).
    Syntax:
    public static string fileField(string $name, string $value='', array $htmlOptions=array ( ))
    Example:
    echo CHtml::fileField('file_name','');

    CHtml::getIdByName()

    Generates a valid HTML ID based on name.
    Syntax:
    public static string getIdByName(string $name)
    Example:
    echo CHtml::getIdByName('title');

    CHtml::image()

    Generates an image tag.
    Syntax:
    public static string image(string $src, string $alt='', array $htmlOptions=array ( ))
    Example:
    echo CHtml::image('http://codexamples.com/themes/responsive/img/logo-dark.png','Code Examples',array(,'onclick'=>'js:alert("Welcome to Codexamples.com.\n This is Codexamples logo")'));