Collective\Html\FormBuilder::select PHP Method

select() public method

Create a select box field.
public select ( string $name, array $list = [], string $selected = null, array $options = [] ) : Illuminate\Support\HtmlString
$name string
$list array
$selected string
$options array
return Illuminate\Support\HtmlString
    public function select($name, $list = [], $selected = null, $options = [])
    {
        // When building a select box the "value" attribute is really the selected one
        // so we will use that when checking the model or session for a value which
        // should provide a convenient method of re-populating the forms on post.
        $selected = $this->getValueAttribute($name, $selected);
        $options['id'] = $this->getIdAttribute($name, $options);
        if (!isset($options['name'])) {
            $options['name'] = $name;
        }
        // We will simply loop through the options and build an HTML value for each of
        // them until we have an array of HTML declarations. Then we will join them
        // all together into one single HTML element that can be put on the form.
        $html = [];
        if (isset($options['placeholder'])) {
            $html[] = $this->placeholderOption($options['placeholder'], $selected);
            unset($options['placeholder']);
        }
        foreach ($list as $value => $display) {
            $html[] = $this->getSelectOption($display, $value, $selected);
        }
        // Once we have all of this HTML, we can join this into a single element after
        // formatting the attributes into an HTML "attributes" string, then we will
        // build out a final select statement, which will contain all the values.
        $options = $this->html->attributes($options);
        $list = implode('', $html);
        return $this->toHtmlString("<select{$options}>{$list}</select>");
    }

Usage Example

Example #1
0
    public function selection($nom, $list = [], $selected = null, $label = null)
    {
        return sprintf('
			<div class="form-group" style="width:200px;">
				%s
				%s
			</div>', $label ? $this->label($nom, $label, ['class' => 'control-label']) : '', parent::select($nom, $list, $selected, ['class' => 'form-control']));
    }
All Usage Examples Of Collective\Html\FormBuilder::select