Skip to content
tanthammar edited this page Apr 8, 2021 · 8 revisions

Select

Extends BaseField

Additional methods

->placeholder(string $placeholder)

  • Display a message when no item is selected.

->options($options)

  • A list or flat key => value based Array, Collection or Closure.
  • @param array|\Closure|\Illuminate\Support\Collection $options
  • OBSERVE: if you use a callable, it will be executed on EVERY re-render of the component! Maybe you should consider setting the $options in mount() instead?
  • You can use a component method that returns an array; ->options($this->someMethod())

Examples

public function fields()
{
    return [
        $this->select(),
        $this->selectAssociative(),
    ];
}

//using labels = key
public function select()
{
    $options = ['Wifi', 'Bluetooth', 'Ethernet'];
    return Select::make('Select')
        ->options($options)
        ->placeholder('Please select an option')
        ->rules(['nullable', Rule::in($options)]);
}

//using associative array
public function selectAssociative()
{
    $options = ['Wifi' => 'wf', 'Bluetooth' => 'bl', 'Ethernet' => 'eth'];
    return Select::make('Select')
        ->options($options)
        ->default('wf')
        ->placeholder('Please select an option')
        ->rules(['nullable', Rule::in(array_values($options))]);
}

Blade component

<x-tall-select :field="$field" />

Styling

Extend Blade component (or override in config file)

Tanthammar\TallForms\Components\Select::class

Config options

//Select placeholders and help, applied as trans(...) or @lang(...)
'select-placeholder' => 'global.select_placeholder', //'Please select an option...'
Clone this wiki locally