Class yii\bootstrap5\Html

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

Public Properties

Hide inherited properties

Property Type Description Defined By
$autoIdPrefix string The prefix to the automatically generated widget IDs. yii\bootstrap5\BaseHtml
$normalizeClassAttribute yii\bootstrap5\BaseHtml

Public Methods

Hide inherited methods

Method Description Defined By
activeStaticControl() Generates a Bootstrap static form control for the given model attribute. yii\bootstrap5\BaseHtml
checkboxList() {@inheritdoc} Pass true in $options['inline'] to generate inline list. yii\bootstrap5\BaseHtml
error() yii\bootstrap5\BaseHtml
radioList() {@inheritdoc} Pass true in $options['inline'] to generate inline list. yii\bootstrap5\BaseHtml
staticControl() Renders Bootstrap static form control. yii\bootstrap5\BaseHtml

Protected Methods

Hide inherited methods

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

Method Details

Hide inherited methods

activeStaticControl() public static method

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

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

See also staticControl().

public static string activeStaticControl ( \yii\base\Model $model, string $attribute, array $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 $model, string $attribute, array $options = []): string
{
    if (isset($options['value'])) {
        $value = $options['value'];
        unset($options['value']);
    } else {
        $value = static::getAttributeValue($model, $attribute);
    }
    return static::staticControl((string) $value, $options);
}

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

                protected static function booleanInput($type, $name, $checked = false, $options = []): string
{
    $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 = $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

Defined in: yii\bootstrap5\BaseHtml::checkboxList()

{@inheritdoc} Pass true in $options['inline'] to generate inline list.

public static string checkboxList ( mixed $name, mixed $selection null, mixed $items = [], mixed $options = [] )
$name mixed
$selection mixed
$items mixed
$options mixed

                public static function checkboxList($name, $selection = null, $items = [], $options = []): string
{
    $inline = ArrayHelper::remove($options, 'inline', false);
    if (!isset($options['item'])) {
        $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
        static::addCssClass($itemOptions, 'form-check-input');
        if (!isset($itemOptions['labelOptions'])) {
            $itemOptions['labelOptions'] = [
                'class' => 'form-check-label',
            ];
        } else {
            static::addCssClass($itemOptions['labelOptions'], 'form-check-label');
        }
        $wrapperOptions = $inline ? [
            'class' => 'form-check form-check-inline',
        ] : [
            'class' => 'form-check',
        ];
        $encode = ArrayHelper::getValue($options, 'encode', true);
        $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $wrapperOptions, $encode) {
            $itemOptions['value'] = $value;
            if (!isset($itemOptions['label'])) {
                $itemOptions['label'] = $encode ? static::encode($label) : $label;
            }
            return static::tag('div', static::checkbox($name, $checked, $itemOptions), $wrapperOptions);
        };
    }
    return parent::checkboxList($name, $selection, $items, $options);
}

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

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

            
getId() protected static method

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

Returns an autogenerated ID

protected static string getId ( )
return string

Autogenerated ID

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

            
radioList() public static method

Defined in: yii\bootstrap5\BaseHtml::radioList()

{@inheritdoc} Pass true in $options['inline'] to generate inline list.

public static string radioList ( mixed $name, mixed $selection null, mixed $items = [], mixed $options = [] )
$name mixed
$selection mixed
$items mixed
$options mixed

                public static function radioList($name, $selection = null, $items = [], $options = []): string
{
    $inline = ArrayHelper::remove($options, 'inline', false);
    if (!isset($options['item'])) {
        $itemOptions = ArrayHelper::remove($options, 'itemOptions', []);
        static::addCssClass($itemOptions, [
            'bootstrap' => 'form-check-input',
        ]);
        if (!isset($itemOptions['labelOptions'])) {
            $itemOptions['labelOptions'] = [
                'class' => 'form-check-label',
            ];
        } else {
            static::addCssClass($itemOptions['labelOptions'], [
                'bootstrap' => 'form-check-label',
            ]);
        }
        $wrapperOptions = $inline ? [
            'class' => 'form-check form-check-inline',
        ] : [
            'class' => 'form-check',
        ];
        $encode = ArrayHelper::getValue($options, 'encode', true);
        $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $wrapperOptions, $encode) {
            $itemOptions['value'] = $value;
            if (!isset($itemOptions['label'])) {
                $itemOptions['label'] = $encode ? static::encode($label) : $label;
            }
            return static::tag('div', static::radio($name, $checked, $itemOptions), $wrapperOptions);
        };
    }
    return parent::radioList($name, $selection, $items, $options);
}

            
staticControl() public static method
public static string staticControl ( string $value, array $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(string $value, array $options = []): string
{
    static::addCssClass($options, 'form-control-plaintext');
    $options['readonly'] = true;
    return static::input('text', null, $value, $options);
}