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 string __toString ( )

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

            
addClass() public method

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

            
addStyle() public method

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. '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 string after ( )

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

            
attribute() public method

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

            
attributes() public method

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

            
before() protected method

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

            
class() public method

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

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

Closing tag.

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

            
disabled() public method

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

            
form() public method

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

            
generateContent() protected method

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

            
getName() protected method

protected string getName ( )

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

            
id() public method

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

            
items() public method

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

            
multiple() public method

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

            
name() public method

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

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

Opening tag.

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

            
options() public method

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

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

            
optionsData() public method

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: `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 {@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);
}

            
prepareAttributes() protected method

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

            
prepend() protected method
protected string prepend ( )

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

            
prompt() public method

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

            
promptOption() public method

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

            
removeStyle() public method

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

            
render() public method
public string render ( )

                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 string renderAttributes ( )

                final protected function renderAttributes(): string
{
    $this->prepareAttributes();
    return Html::renderTagAttributes($this->attributes);
}

            
renderTag() protected method
protected string renderTag ( )

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

            
required() public method

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

            
size() public method

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

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

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

            
unselectValue() public method

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

            
value() public method

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

            
values() public method

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