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 string __toString ( ) |
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 Yiisoft\Html\Tag\Select addAttributes ( array $attributes ) | ||
| $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 Yiisoft\Html\Tag\Select addClass ( \BackedEnum|string|null $class ) | ||
| $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 Yiisoft\Html\Tag\Select addStyle ( string|string[] $style, boolean $overwrite = true ) | ||
| $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 string after ( ) |
protected function after(): string
{
return '';
}
Defined in: Yiisoft\Html\Tag\Base\Tag::attribute()
Set attribute value.
| public Yiisoft\Html\Tag\Select attribute ( string $name, mixed $value ) | ||
| $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 Yiisoft\Html\Tag\Select attributes ( array $attributes ) | ||
| $attributes | array |
Name-value set of attributes. |
final public function attributes(array $attributes): static
{
$new = clone $this;
$new->attributes = $attributes;
return $new;
}
| protected string before ( ) |
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 Yiisoft\Html\Tag\Select class ( \BackedEnum|string|null $class ) | ||
| $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 string close ( ) | ||
| return | string |
Closing tag. |
|---|---|---|
final public function close(): string
{
return '</' . $this->getName() . '>';
}
| public self disabled ( boolean $disabled = true ) | ||
| $disabled | boolean |
Whether select input is disabled. |
public function disabled(bool $disabled = true): self
{
$new = clone $this;
$new->attributes['disabled'] = $disabled;
return $new;
}
| public self form ( string|null $formId ) | ||
| $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 string generateContent ( ) |
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 Yiisoft\Html\Tag\Select id ( string|null $id ) | ||
| $id | string|null |
Tag ID. |
final public function id(?string $id): static
{
$new = clone $this;
$new->attributes['id'] = $id;
return $new;
}
| public self items ( Yiisoft\Html\Tag\Optgroup|Yiisoft\Html\Tag\Option $items ) | ||
| $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 self multiple ( boolean $multiple = true ) | ||
| $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 self name ( string|null $name ) | ||
| $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 string open ( ) | ||
| return | string |
Opening tag. |
|---|---|---|
final public function open(): string
{
return '<' . $this->getName() . $this->renderAttributes() . '>' . $this->prepend();
}
| public self options ( Yiisoft\Html\Tag\Option $options ) | ||
| $options | Yiisoft\Html\Tag\Option | |
public function options(Option ...$options): self
{
return $this->items(...$options);
}
| public self optionsData ( array $data, boolean $encode = true, array[] $optionsAttributes = [], array[] $groupsAttributes = [] ) | ||
| $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 {@see $data}. |
| $groupsAttributes | array[] |
Array of group attribute sets indexed by group labels from {@see $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 void prepareAttributes ( ) |
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 string prepend ( ) |
protected function prepend(): string
{
return '';
}
| public self prompt ( string|null $text ) | ||
| $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 self promptOption ( Yiisoft\Html\Tag\Option|null $option ) | ||
| $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 Yiisoft\Html\Tag\Select removeStyle ( string|string[] $properties ) | ||
| $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 string render ( ) |
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 string renderAttributes ( ) |
final protected function renderAttributes(): string
{
$this->prepareAttributes();
return Html::renderTagAttributes($this->attributes);
}
Defined in: Yiisoft\Html\Tag\Base\NormalTag::renderTag()
| protected string renderTag ( ) |
final protected function renderTag(): string
{
return $this->open() . $this->generateContent() . $this->close();
}
| public self required ( boolean $required = true ) | ||
| $required | boolean |
Whether select input is required. |
public function required(bool $required = true): self
{
$new = clone $this;
$new->attributes['required'] = $required;
return $new;
}
| public self size ( integer|null $size ) | ||
| $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 Yiisoft\Html\Tag\Select tag ( ) |
final public static function tag(): static
{
return new static();
}
Defined in: Yiisoft\Html\Tag\Base\Tag::unionAttributes()
Union attributes with a new set.
| public Yiisoft\Html\Tag\Select unionAttributes ( array $attributes ) | ||
| $attributes | array |
Name-value set of attributes. |
final public function unionAttributes(array $attributes): static
{
$new = clone $this;
$new->attributes += $attributes;
return $new;
}
| public self unselectValue ( boolean|float|integer|string|\Stringable|null $value ) | ||
| $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 self value ( \Stringable|boolean|float|integer|string|\BackedEnum|null $value ) | ||
| $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.