Class yii\bootstrap4\Html

Inheritanceyii\bootstrap4\Html » yii\bootstrap4\BaseHtml » yii\helpers\Html
Source Code https://github.com/yiisoft/yii2-bootstrap4/blob/master/src/Html.php

Html is an enhanced version of \yii\helpers\Html helper class dedicated to the Bootstrap needs.

This class inherits all functionality available at \yii\helpers\Html and can be used as substitute.

Attention: do not confuse yii\bootstrap4\Html and \yii\helpers\Html, be careful in which class you are using inside your views.

Public Properties

Hide inherited properties

Property Type Description Defined By
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\bootstrap4\BaseHtml
$dataAttributes array List of tag attributes that should be specially handled when their values are of array type. yii\bootstrap4\BaseHtml
$normalizeClassAttribute boolean Whether to removes duplicate class names in tag attribute class yii\bootstrap4\BaseHtml

Public Methods

Hide inherited methods

Method Description Defined By
activeStaticControl() Generates a Bootstrap static form control for the given model attribute. yii\bootstrap4\BaseHtml
checkboxList() yii\bootstrap4\BaseHtml
error() yii\bootstrap4\BaseHtml
radioList() yii\bootstrap4\BaseHtml
staticControl() Renders Bootstrap static form control. yii\bootstrap4\BaseHtml

Protected Methods

Hide inherited methods

Method Description Defined By
booleanInput() yii\bootstrap4\BaseHtml
getId() Returns an autogenerated ID yii\bootstrap4\BaseHtml

Method Details

Hide inherited methods

activeStaticControl() public static method

Defined in: yii\bootstrap4\BaseHtml::activeStaticControl()

Generates a Bootstrap static form control for the given model attribute.

See also staticControl().

public static string activeStaticControl ( $model, $attribute, $options = [] )
$model \yii\base\Model

The model object.

$attribute string

The attribute name or expression. See getAttributeName() for the format about attribute expression.

$options array

The tag options in terms of name-value pairs. See staticControl() for details.

return string

Generated HTML

                public static function activeStaticControl($model, $attribute, $options = [])
{
    if (isset($options['value'])) {
        $value = $options['value'];
        unset($options['value']);
    } else {
        $value = static::getAttributeValue($model, $attribute);
    }
    return static::staticControl($value, $options);
}

            
booleanInput() protected static method
protected static void booleanInput ( $type, $name, $checked false, $options = [] )
$type
$name
$checked
$options

                protected static function booleanInput($type, $name, $checked = false, $options = [])
{
    $options['checked'] = (bool)$checked;
    $value = array_key_exists('value', $options) ? $options['value'] : '1';
    if (isset($options['uncheck'])) {
        // add a hidden field so that if the checkbox is not selected, it still submits a value
        $hiddenOptions = [];
        if (isset($options['form'])) {
            $hiddenOptions['form'] = $options['form'];
        }
        $hidden = static::hiddenInput($name, $options['uncheck'], $hiddenOptions);
        unset($options['uncheck']);
    } else {
        $hidden = '';
    }
    if (isset($options['label'])) {
        $label = $options['label'];
        $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
        unset($options['label'], $options['labelOptions']);
        if (!isset($options['id'])) {
            $options['id'] = static::getId();
        }
        $input = static::input($type, $name, $value, $options);
        if (isset($labelOptions['wrapInput']) && $labelOptions['wrapInput']) {
            unset($labelOptions['wrapInput']);
            $content = static::label($input . $label, $options['id'], $labelOptions);
        } else {
            $content = $input . "\n" . static::label($label, $options['id'], $labelOptions);
        }
        return $hidden . $content;
    }
    return $hidden . static::input($type, $name, $value, $options);
}

            
checkboxList() public static method
public static void checkboxList ( $name, $selection null, $items = [], $options = [] )
$name
$selection
$items
$options

                public static function checkboxList($name, $selection = null, $items = [], $options = [])
{
    if (!isset($options['item'])) {
        $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
        $encode = ArrayHelper::getValue($options, 'encode', true);
        $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
            $options = array_merge([
                'class' => 'form-check-input',
                'label' => $encode ? static::encode($label) : $label,
                'labelOptions' => ['class' => 'form-check-label'],
                'value' => $value
            ], $itemOptions);
            return '<div class="form-check">' . Html::checkbox($name, $checked, $options) . '</div>';
        };
    }
    return parent::checkboxList($name, $selection, $items, $options);
}

            
error() public static method
public static void error ( $model, $attribute, $options = [] )
$model
$attribute
$options

                public static function error($model, $attribute, $options = [])
{
    if (!array_key_exists('class', $options)) {
        $options['class'] = ['invalid-feedback'];
    }
    return parent::error($model, $attribute, $options);
}

            
getId() protected static method

Defined in: yii\bootstrap4\BaseHtml::getId()

Returns an autogenerated ID

protected static string getId ( )
return string

Autogenerated ID

                protected static function getId()
{
    return static::$autoIdPrefix . static::$counter++;
}

            
radioList() public static method
public static void radioList ( $name, $selection null, $items = [], $options = [] )
$name
$selection
$items
$options

                public static function radioList($name, $selection = null, $items = [], $options = [])
{
    if (!isset($options['item'])) {
        $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
        $encode = ArrayHelper::getValue($options, 'encode', true);
        $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
            $options = array_merge([
                'class' => 'form-check-input',
                'label' => $encode ? static::encode($label) : $label,
                'labelOptions' => ['class' => 'form-check-label'],
                'value' => $value
            ], $itemOptions);
            return '<div class="form-check">' . static::radio($name, $checked, $options) . '</div>';
        };
    }
    return parent::radioList($name, $selection, $items, $options);
}

            
staticControl() public static method
public static string staticControl ( $value, $options = [] )
$value string

Static control value.

$options array

The tag options in terms of name-value pairs. These will be rendered as the attributes of the resulting tag. There are also a special options:

return string

Generated HTML

                public static function staticControl($value, $options = [])
{
    static::addCssClass($options, 'form-control-plaintext');
    $value = (string)$value;
    $options['readonly'] = true;
    return static::input('text', null, $value, $options);
}