yii\helpers\BaseHtml::booleanInput PHP Method

booleanInput() protected static method

Generates a boolean input.
Since: 2.0.9
protected static booleanInput ( string $type, string $name, boolean $checked = false, array $options = [] ) : string
$type string the input type. This can be either `radio` or `checkbox`.
$name string the name attribute.
$checked boolean whether the checkbox should be checked.
$options array the tag options in terms of name-value pairs. The following options are specially handled: - uncheck: string, the value associated with the uncheck state of the checkbox. When this attribute is present, a hidden input will be generated so that if the checkbox is not checked and is submitted, the value of this attribute will still be submitted to the server via the hidden input. - label: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass in HTML code such as an image tag. If this is is coming from end users, you should [[encode()]] it to prevent XSS attacks. When this option is specified, the checkbox will be enclosed by a label tag. - labelOptions: array, the HTML attributes for the label tag. Do not set this option unless you set the "label" option. The rest of the options will be rendered as the attributes of the resulting checkbox tag. The values will be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. See [[renderTagAttributes()]] for details on how attributes are being rendered.
return string the generated checkbox tag
    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
            $hidden = static::hiddenInput($name, $options['uncheck']);
            unset($options['uncheck']);
        } else {
            $hidden = '';
        }
        if (isset($options['label'])) {
            $label = $options['label'];
            $labelOptions = isset($options['labelOptions']) ? $options['labelOptions'] : [];
            unset($options['label'], $options['labelOptions']);
            $content = static::label(static::input($type, $name, $value, $options) . ' ' . $label, null, $labelOptions);
            return $hidden . $content;
        } else {
            return $hidden . static::input($type, $name, $value, $options);
        }
    }