Final Class Yiisoft\Html\Widget\CheckboxList\CheckboxList
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
CheckboxList represents a list of checkboxes and their corresponding labels.
Public Methods
Method Details
| public mixed __construct ( string $name ) | ||
| $name | string | |
public function __construct(
private string $name,
) {}
| public self addCheckboxAttributes ( array $attributes ) | ||
| $attributes | array | |
public function addCheckboxAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxAttributes = array_merge($new->checkboxAttributes, $attributes);
return $new;
}
| public self addCheckboxLabelAttributes ( array $attributes ) | ||
| $attributes | array | |
public function addCheckboxLabelAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxLabelAttributes = array_merge($new->checkboxLabelAttributes, $attributes);
return $new;
}
| public self addCheckboxWrapClass ( ?string $class ) | ||
| $class | ?string | |
public function addCheckboxWrapClass(?string ...$class): self
{
$new = clone $this;
Html::addCssClass(
$new->checkboxWrapAttributes,
array_filter($class, static fn($c) => $c !== null),
);
return $new;
}
| public self addIndividualInputAttributes ( array[] $attributes ) | ||
| $attributes | array[] | |
public function addIndividualInputAttributes(array $attributes): self
{
$new = clone $this;
$new->individualInputAttributes = array_replace($new->individualInputAttributes, $attributes);
return $new;
}
| public self afterCheckbox ( string|\ | ||
| $content | string|\ |
Content to be rendered after each checkbox input. |
public function afterCheckbox(string|Stringable $content): self
{
$new = clone $this;
$new->afterCheckbox = $content;
return $new;
}
| public self beforeCheckbox ( string|\ | ||
| $content | string|\ |
Content to be rendered before each checkbox input. |
public function beforeCheckbox(string|Stringable $content): self
{
$new = clone $this;
$new->beforeCheckbox = $content;
return $new;
}
| public self checkboxAttributes ( array $attributes ) | ||
| $attributes | array | |
public function checkboxAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxAttributes = $attributes;
return $new;
}
| public self checkboxLabelAttributes ( array $attributes ) | ||
| $attributes | array | |
public function checkboxLabelAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxLabelAttributes = $attributes;
return $new;
}
| public self checkboxLabelWrap ( boolean $wrap ) | ||
| $wrap | boolean | |
public function checkboxLabelWrap(bool $wrap): self
{
$new = clone $this;
$new->checkboxLabelWrap = $wrap;
return $new;
}
| public self checkboxWrapAttributes ( array $attributes ) | ||
| $attributes | array | |
public function checkboxWrapAttributes(array $attributes): self
{
$new = clone $this;
$new->checkboxWrapAttributes = $attributes;
return $new;
}
| public self checkboxWrapClass ( ?string $class ) | ||
| $class | ?string | |
public function checkboxWrapClass(?string ...$class): self
{
$new = clone $this;
$new->checkboxWrapAttributes['class'] = array_filter($class, static fn($c) => $c !== null);
return $new;
}
| public self checkboxWrapTag ( ?string $name ) | ||
| $name | ?string | |
public function checkboxWrapTag(?string $name): self
{
$new = clone $this;
$new->checkboxWrapTag = $name;
return $new;
}
| public self containerAttributes ( array $attributes ) | ||
| $attributes | array | |
public function containerAttributes(array $attributes): self
{
$new = clone $this;
$new->containerAttributes = $attributes;
return $new;
}
| public self containerTag ( ?string $name ) | ||
| $name | ?string | |
public function containerTag(?string $name): self
{
$new = clone $this;
$new->containerTag = $name;
return $new;
}
Deprecated
Use the constructor instead.
| public static self create ( string $name ) | ||
| $name | string | |
public static function create(string $name): self
{
return new self($name);
}
| public self disabled ( boolean $disabled = true ) | ||
| $disabled | boolean | |
public function disabled(bool $disabled = true): self
{
$new = clone $this;
$new->checkboxAttributes['disabled'] = $disabled;
return $new;
}
| public self form ( ?string $formId ) | ||
| $formId | ?string | |
public function form(?string $formId): self
{
$new = clone $this;
$new->checkboxAttributes['form'] = $formId;
return $new;
}
| public self individualInputAttributes ( array[] $attributes ) | ||
| $attributes | array[] | |
public function individualInputAttributes(array $attributes): self
{
$new = clone $this;
$new->individualInputAttributes = $attributes;
return $new;
}
| public self itemFormatter ( ?\ | ||
| $formatter | ?\ |
|
public function itemFormatter(?Closure $formatter): self
{
$new = clone $this;
$new->itemFormatter = $formatter;
return $new;
}
| public self items ( string[] $items, boolean $encodeLabels = true ) | ||
| $items | string[] | |
| $encodeLabels | boolean |
Whether labels should be encoded. |
public function items(array $items, bool $encodeLabels = true): self
{
$new = clone $this;
$new->items = $items;
$new->encodeLabels = $encodeLabels;
return $new;
}
Fills items from an array provided. Array values are used for both input labels and input values.
| public self itemsFromValues ( boolean[]|float[]|integer[]|string[]|\ | ||
| $values | boolean[]|float[]|integer[]|string[]|\ |
|
| $encodeLabels | boolean |
Whether labels should be encoded. |
public function itemsFromValues(array $values, bool $encodeLabels = true): self
{
$values = array_map('\strval' , $values);
return $this->items(
array_combine($values, $values),
$encodeLabels,
);
}
| public self name ( string $name ) | ||
| $name | string | |
public function name(string $name): self
{
$new = clone $this;
$new->name = $name;
return $new;
}
| public self readonly ( boolean $readonly = true ) | ||
| $readonly | boolean | |
public function readonly(bool $readonly = true): self
{
$new = clone $this;
$new->checkboxAttributes['readonly'] = $readonly;
return $new;
}
| public string render ( ) |
public function render(): string
{
$name = Html::getArrayableName($this->name);
if ($this->checkboxWrapTag === null) {
$beforeCheckbox = '';
$afterCheckbox = '';
} else {
$beforeCheckbox = Html::openTag($this->checkboxWrapTag, $this->checkboxWrapAttributes) . "\n" ;
$afterCheckbox = "\n" . Html::closeTag($this->checkboxWrapTag);
}
$lines = [];
$index = 0;
foreach ($this->items as $value => $label) {
$item = new CheckboxItem(
$index,
$name,
$value,
ArrayHelper::isIn($value, $this->values),
array_merge(
$this->checkboxAttributes,
$this->individualInputAttributes[$value] ?? [],
['name' => $name, 'value' => $value],
),
$label,
$this->encodeLabels,
$this->checkboxLabelAttributes,
$this->checkboxLabelWrap,
);
$lines[] = $beforeCheckbox . $this->formatItem($item) . $afterCheckbox;
$index++;
}
$html = [];
if ($this->uncheckValue !== null) {
$html[] = $this->renderUncheckInput($this->uncheckValue);
}
if (!empty($this->containerTag)) {
$html[] = Html::openTag($this->containerTag, $this->containerAttributes);
}
if ($lines) {
$html[] = implode($this->separator, $lines);
}
if (!empty($this->containerTag)) {
$html[] = Html::closeTag($this->containerTag);
}
return implode("\n" , $html);
}
| public self separator ( string $separator ) | ||
| $separator | string | |
public function separator(string $separator): self
{
$new = clone $this;
$new->separator = $separator;
return $new;
}
| public self uncheckValue ( boolean|float|integer|string|\ | ||
| $value | boolean|float|integer|string|\ |
|
public function uncheckValue(bool|float|int|string|Stringable|null $value): self
{
$new = clone $this;
$new->uncheckValue = $value === null ? null : (string) $value;
return $new;
}
| public self value ( boolean|string|integer|float|\ | ||
| $value | boolean|string|integer|float|\ |
|
public function value(bool|string|int|float|Stringable|BackedEnum ...$value): self
{
$new = clone $this;
$new->values = array_map(
static fn($v): string => (string) ($v instanceof BackedEnum ? $v->value : $v),
array_values($value),
);
return $new;
}
| public self values ( iterable $values ) | ||
| $values | iterable | |
public function values(iterable $values): self
{
$values = is_array($values) ? $values : iterator_to_array($values);
return $this->value(...$values);
}
| public self withoutContainer ( ) |
public function withoutContainer(): self
{
$new = clone $this;
$new->containerTag = null;
return $new;
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.