0 follower

Final Class Yiisoft\Html\Tag\Select

InheritanceYiisoft\Html\Tag\Select » Yiisoft\Html\Tag\Base\NormalTag » Yiisoft\Html\Tag\Base\Tag
ImplementsYiisoft\Html\NoEncodeStringableInterface

The select element represents a control for selecting amongst a set of options.

Psalm Types

Name Value
OptionsData array<array-key, string|array<array-key, string>>

Protected Properties

Hide inherited properties

Property Type Description Defined By
$attributes array Yiisoft\Html\Tag\Base\Tag

Method Details

Hide inherited methods

__toString() public method
public __toString( ): string

                final public function __toString(): string
{
    return $this->render();
}

            
addAttributes() public method

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;
}

            
addClass() public method

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;
}

            
addStyle() public method

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. 'width: 100px; height: 200px') or array (e.g. ['width' => '100px', 'height' => '200px']).

$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;
}

            
after() protected method
protected after( ): string

                protected function after(): string
{
    return '';
}

            
attribute() public method

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;
}

            
attributes() public method

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;
}

            
before() protected method

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";
}

            
class() public method

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;
}

            
close() public method
public close( ): string
return string

Closing tag.

                final public function close(): string
{
    return '</' . $this->getName() . '>';
}

            
disabled() public method

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;
}

            
form() public method

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;
}

            
generateContent() protected method

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"
        : '';
}

            
getName() protected method

protected getName( ): string

                protected function getName(): string
{
    return 'select';
}

            
id() public method

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;
}

            
items() public method

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;
}

            
multiple() public method

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;
}

            
name() public method

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;
}

            
open() public method
public open( ): string
return string

Opening tag.

                final public function open(): string
{
    return '<' . $this->getName() . $this->renderAttributes() . '>' . $this->prepend();
}

            
options() public method

public options( Yiisoft\Html\Tag\Option $options ): self
$options Yiisoft\Html\Tag\Option

                public function options(Option ...$options): self
{
    return $this->items(...$options);
}

            
optionsData() public method

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: `php [

'1' => 'Santiago',
'2' => 'Concepcion',
'3' => 'Chillan',
'4' => 'Moscow'
'5' => 'San Petersburg',
'6' => 'Novosibirsk',
'7' => 'Ekaterinburg'

]; `

Example with options groups: `php [

'1' => [
    '1' => 'Santiago',
    '2' => 'Concepcion',
    '3' => 'Chillan',
],
'2' => [
    '4' => 'Moscow',
    '5' => 'San Petersburg',
    '6' => 'Novosibirsk',
    '7' => 'Ekaterinburg'
],

]; `

$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);
}

            
prepareAttributes() protected method

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']);
    }
}

            
prepend() protected method
protected prepend( ): string

                protected function prepend(): string
{
    return '';
}

            
prompt() public method

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;
}

            
promptOption() public method

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;
}

            
removeStyle() public method

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;
}

            
render() public method
public render( ): string

                final public function render(): string
{
    return $this->before() . $this->renderTag() . $this->after();
}

            
renderAttributes() protected method

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);
}

            
renderTag() protected method
protected renderTag( ): string

                final protected function renderTag(): string
{
    return $this->open() . $this->generateContent() . $this->close();
}

            
required() public method

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;
}

            
size() public method

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;
}

            
tag() public static method
public static tag( ): Yiisoft\Html\Tag\Select

                final public static function tag(): static
{
    return new static();
}

            
unionAttributes() public method

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;
}

            
unselectValue() public method

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;
}

            
value() public method

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;
}

            
values() public method

public values( iterable $values ): self
$values iterable

                public function values(iterable $values): self
{
    $values = is_array($values) ? $values : iterator_to_array($values);
    return $this->value(...$values);
}