Final Class Yiisoft\Html\Tag\Select
| Inheritance | Yiisoft\Html\Tag\Select » Yiisoft\Html\Tag\Base\NormalTag » Yiisoft\Html\Tag\Base\Tag |
|---|---|
| Implements | Yiisoft\Html\NoEncodeStringableInterface |
The select element represents a control for selecting amongst a set of options.
Protected Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $attributes | array | Yiisoft\Html\Tag\Base\Tag |
Public Methods
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| after() | Yiisoft\Html\Tag\Base\Tag | |
| before() | Yiisoft\Html\Tag\Select | |
| generateContent() | Yiisoft\Html\Tag\Select | |
| getName() | Yiisoft\Html\Tag\Select | |
| prepareAttributes() | Yiisoft\Html\Tag\Select | |
| prepend() | Yiisoft\Html\Tag\Base\NormalTag | |
| renderAttributes() | Render the current tag attributes. | Yiisoft\Html\Tag\Base\Tag |
| renderTag() | Yiisoft\Html\Tag\Base\NormalTag |
Method Details
Defined in: Yiisoft\Html\Tag\Base\Tag::__toString()
| public __toString( ): string |
final public function __toString(): string
{
return $this->render();
}
Defined in: Yiisoft\Html\Tag\Base\Tag::addAttributes()
Add a set of attributes to existing tag attributes.
Same named attributes are replaced.
| public addAttributes( array $attributes ): Yiisoft\Html\Tag\Select | ||
| $attributes | array |
Name-value set of attributes. |
final public function addAttributes(array $attributes): static
{
$new = clone $this;
$new->attributes = array_merge($new->attributes, $attributes);
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::addClass()
Add one or more CSS classes to the tag.
| public addClass( \BackedEnum|string|null $class ): Yiisoft\Html\Tag\Select | ||
| $class | \BackedEnum|string|null |
One or many CSS classes. |
final public function addClass(BackedEnum|string|null ...$class): static
{
$new = clone $this;
Html::addCssClass($new->attributes, $class);
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::addStyle()
Add CSS styles to the tag.
See also Yiisoft\Html\Html::addCssStyle().
| public addStyle( string|string[] $style, boolean $overwrite = true ): Yiisoft\Html\Tag\Select | ||
| $style | string|string[] |
The new style string (e.g. |
| $overwrite | boolean |
Whether to overwrite existing CSS properties if the new style contain them too. |
final public function addStyle(array|string $style, bool $overwrite = true): static
{
$new = clone $this;
Html::addCssStyle($new->attributes, $style, $overwrite);
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::after()
| protected after( ): string |
protected function after(): string
{
return '';
}
Defined in: Yiisoft\Html\Tag\Base\Tag::attribute()
Set attribute value.
| public attribute( string $name, mixed $value ): Yiisoft\Html\Tag\Select | ||
| $name | string |
Name of the attribute. |
| $value | mixed |
Value of the attribute. |
final public function attribute(string $name, mixed $value): static
{
$new = clone $this;
$new->attributes[$name] = $value;
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::attributes()
Replace attributes with a new set.
| public attributes( array $attributes ): Yiisoft\Html\Tag\Select | ||
| $attributes | array |
Name-value set of attributes. |
final public function attributes(array $attributes): static
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
| protected before( ): string |
protected function before(): string
{
$name = (string) ($this->attributes['name'] ?? '');
if (
empty($name)
|| (
$this->unselectValue === null
&& empty($this->attributes['multiple'])
)
) {
return '';
}
$input = Input::hidden(
Html::getNonArrayableName($name),
(string) $this->unselectValue,
);
// Make sure disabled input is not sending any value.
if (!empty($this->attributes['disabled'])) {
$input = $input->attribute('disabled', $this->attributes['disabled']);
}
if (!empty($this->attributes['form'])) {
$input = $input->attribute('form', $this->attributes['form']);
}
return $input->render() . "\n";
}
Defined in: Yiisoft\Html\Tag\Base\Tag::class()
Replace current tag CSS classes with a new set of classes.
| public class( \BackedEnum|string|null $class ): Yiisoft\Html\Tag\Select | ||
| $class | \BackedEnum|string|null |
One or many CSS classes. |
final public function class(BackedEnum|string|null ...$class): static
{
$new = clone $this;
unset($new->attributes['class']);
Html::addCssClass($new->attributes, $class);
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::close()
| public close( ): string | ||
| return | string |
Closing tag. |
|---|---|---|
final public function close(): string
{
return '</' . $this->getName() . '>';
}
| public disabled( boolean $disabled = true ): self | ||
| $disabled | boolean |
Whether select input is disabled. |
public function disabled(bool $disabled = true): self
{
$new = clone $this;
$new->attributes['disabled'] = $disabled;
return $new;
}
| public form( string|null $formId ): self | ||
| $formId | string|null |
ID of the form the select belongs to. |
public function form(?string $formId): self
{
$new = clone $this;
$new->attributes['form'] = $formId;
return $new;
}
| protected generateContent( ): string |
protected function generateContent(): string
{
$items = $this->items;
if ($this->prompt) {
array_unshift($items, $this->prompt);
}
$items = array_map(
fn($item) => $item instanceof Optgroup
? $item->selection(...$this->values)
: $item->selected(in_array($item->getValue(), $this->values, true)),
$items,
);
return $items
? "\n" . implode("\n", $items) . "\n"
: '';
}
Defined in: Yiisoft\Html\Tag\Base\Tag::id()
Set tag ID.
| public id( string|null $id ): Yiisoft\Html\Tag\Select | ||
| $id | string|null |
Tag ID. |
final public function id(?string $id): static
{
$new = clone $this;
$new->attributes['id'] = $id;
return $new;
}
| public items( Yiisoft\Html\Tag\Optgroup|Yiisoft\Html\Tag\Option $items ): self | ||
| $items | Yiisoft\Html\Tag\Optgroup|Yiisoft\Html\Tag\Option |
Select options or option groups. |
public function items(Optgroup|Option ...$items): self
{
$new = clone $this;
$new->items = $items;
return $new;
}
| public multiple( boolean $multiple = true ): self | ||
| $multiple | boolean |
Whether to allow selecting multiple values. |
public function multiple(bool $multiple = true): self
{
$new = clone $this;
$new->attributes['multiple'] = $multiple;
return $new;
}
| public name( string|null $name ): self | ||
| $name | string|null |
Name of the select input. |
public function name(?string $name): self
{
$new = clone $this;
$new->attributes['name'] = $name;
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::open()
| public open( ): string | ||
| return | string |
Opening tag. |
|---|---|---|
final public function open(): string
{
return '<' . $this->getName() . $this->renderAttributes() . '>' . $this->prepend();
}
| public options( Yiisoft\Html\Tag\Option $options ): self | ||
| $options | Yiisoft\Html\Tag\Option | |
public function options(Option ...$options): self
{
return $this->items(...$options);
}
| public optionsData( array $data, boolean $encode = true, array[] $optionsAttributes = [], array[] $groupsAttributes = [] ): self | ||
| $data | array |
Options data. The array keys are option values, and the array values are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). For each sub-array, an option group will be generated whose label is the key associated with the sub-array. Example:
];
Example with options groups:
];
|
| $encode | boolean |
Whether option content should be HTML-encoded. |
| $optionsAttributes | array[] |
Array of option attribute sets indexed by option values from $data. |
| $groupsAttributes | array[] |
Array of group attribute sets indexed by group labels from $data. |
public function optionsData(
array $data,
bool $encode = true,
array $optionsAttributes = [],
array $groupsAttributes = [],
): self {
$items = [];
foreach ($data as $value => $content) {
if (is_array($content)) {
$items[] = Optgroup::tag()
->label((string) $value)
->addAttributes($groupsAttributes[$value] ?? [])
->optionsData($content, $encode, $optionsAttributes);
} else {
$items[] = Option::tag()
->attributes($optionsAttributes[$value] ?? [])
->value($value)
->content($content)
->encode($encode);
}
}
return $this->items(...$items);
}
| protected prepareAttributes( ): void |
protected function prepareAttributes(): void
{
if (!empty($this->attributes['multiple']) && !empty($this->attributes['name'])) {
$this->attributes['name'] = Html::getArrayableName((string) $this->attributes['name']);
}
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::prepend()
| protected prepend( ): string |
protected function prepend(): string
{
return '';
}
| public prompt( string|null $text ): self | ||
| $text | string|null |
Text of the option that has dummy value and is rendered as an invitation to select a value. |
public function prompt(?string $text): self
{
$new = clone $this;
$new->prompt = $text === null ? null : Option::tag()
->value('')
->content($text);
return $new;
}
| public promptOption( Yiisoft\Html\Tag\Option|null $option ): self | ||
| $option | Yiisoft\Html\Tag\Option|null |
Option that has dummy value and is rendered as an invitation to select a value. |
public function promptOption(?Option $option): self
{
$new = clone $this;
$new->prompt = $option;
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::removeStyle()
Remove CSS styles from the tag.
See also Yiisoft\Html\Html::removeCssStyle().
| public removeStyle( string|string[] $properties ): Yiisoft\Html\Tag\Select | ||
| $properties | string|string[] |
The CSS properties to be removed. You may use a string if you are removing a single property. |
final public function removeStyle(string|array $properties): static
{
$new = clone $this;
Html::removeCssStyle($new->attributes, $properties);
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\Tag::render()
| public render( ): string |
final public function render(): string
{
return $this->before() . $this->renderTag() . $this->after();
}
Defined in: Yiisoft\Html\Tag\Base\Tag::renderAttributes()
Render the current tag attributes.
See also Yiisoft\Html\Html::renderTagAttributes().
| protected renderAttributes( ): string |
final protected function renderAttributes(): string
{
$this->prepareAttributes();
return Html::renderTagAttributes($this->attributes);
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::renderTag()
| protected renderTag( ): string |
final protected function renderTag(): string
{
return $this->open() . $this->generateContent() . $this->close();
}
| public required( boolean $required = true ): self | ||
| $required | boolean |
Whether select input is required. |
public function required(bool $required = true): self
{
$new = clone $this;
$new->attributes['required'] = $required;
return $new;
}
| public size( integer|null $size ): self | ||
| $size | integer|null |
The number of options to show to the user. |
public function size(?int $size): self
{
$new = clone $this;
$new->attributes['size'] = $size;
return $new;
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::tag()
| public static tag( ): Yiisoft\Html\Tag\Select |
final public static function tag(): static
{
return new static();
}
Defined in: Yiisoft\Html\Tag\Base\Tag::unionAttributes()
Union attributes with a new set.
| public unionAttributes( array $attributes ): Yiisoft\Html\Tag\Select | ||
| $attributes | array |
Name-value set of attributes. |
final public function unionAttributes(array $attributes): static
{
$new = clone $this;
$new->attributes += $attributes;
return $new;
}
| public unselectValue( boolean|float|integer|string|\Stringable|null $value ): self | ||
| $value | boolean|float|integer|string|\Stringable|null | |
public function unselectValue(bool|float|int|string|Stringable|null $value): self
{
$new = clone $this;
$new->unselectValue = $value === null ? null : (string) $value;
return $new;
}
| public value( \Stringable|boolean|float|integer|string|\BackedEnum|null $value ): self | ||
| $value | \Stringable|boolean|float|integer|string|\BackedEnum|null | |
public function value(Stringable|bool|float|int|string|BackedEnum|null ...$value): self
{
$values = array_filter(
$value,
static fn(mixed $v): bool => $v !== null,
);
$values = array_map(
static function (Stringable|bool|float|int|string|BackedEnum $v): string {
return (string) ($v instanceof BackedEnum ? $v->value : $v);
},
$values,
);
$new = clone $this;
$new->values = $values;
return $new;
}
Signup or Login in order to comment.