Is it possible to set default selected value?

Is it possible to set default selected option in select field without using db table’s default()? This is basically for adding selected attribute into the <option> html tag. In addition to this, any way to achieve the same for select field with relationship?
Solution:
```php Select::make('select') ->options([ 1 => 'Option 1', 2 => 'Option 2',...
Jump to solution
8 Replies
Solution
LeandroFerreira
LeandroFerreira16mo ago
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
])
->default(1)
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
])
->default(1)
?
vandershraaf
vandershraafOP16mo ago
That works. Thanks!!
Xavi
Xavi16mo ago
Leandro i try this
public function form(Form $form): Form
{
return $form
->schema([
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
])
->default(2)
->placeholder('Select an option')
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Select::make('select')
->options([
1 => 'Option 1',
2 => 'Option 2',
])
->default(2)
->placeholder('Select an option')
]);
}
and doesn't works. How can i do? I use Form Builder onlye
Xavi
Xavi16mo ago
Yes, there is. But i need to fill select with values based on three booleans fields, and i don't know how
LeandroFerreira
LeandroFerreira16mo ago
Share the code you are trying to do please
Xavi
Xavi16mo ago
i can do using this. May be it's not the best solution but it works. On mount this code
public function mount(): void
{
$data = $this->media->attributesToArray();

if ($this->media->options->links_follow) {
$data['rels'][] = 1;
}

if ($this->media->options->links_nofollow) {
$data['rels'][] = 2;
}

if ($this->media->options->links_sponsored) {
$data['rels'][] = 3;
}


$this->form->fill($data);

}
public function mount(): void
{
$data = $this->media->attributesToArray();

if ($this->media->options->links_follow) {
$data['rels'][] = 1;
}

if ($this->media->options->links_nofollow) {
$data['rels'][] = 2;
}

if ($this->media->options->links_sponsored) {
$data['rels'][] = 3;
}


$this->form->fill($data);

}
on save method
public function save(): void
{
$data = $this->form->getState();

$data['options'] = [
'links_follow' => false,
'links_nofollow' => false,
'links_sponsored' => false,
];

foreach ($data['rels'] as $rel) {
if ($rel >= 1 && $rel <= 3) {
$data['options']['links_follow'] = $rel == 1;
$data['options']['links_nofollow'] = $rel == 2;
$data['options']['links_sponsored'] = $rel == 3;
}
}

$this->media->update($data);
$this->media->options->update($data['options']);
}
public function save(): void
{
$data = $this->form->getState();

$data['options'] = [
'links_follow' => false,
'links_nofollow' => false,
'links_sponsored' => false,
];

foreach ($data['rels'] as $rel) {
if ($rel >= 1 && $rel <= 3) {
$data['options']['links_follow'] = $rel == 1;
$data['options']['links_nofollow'] = $rel == 2;
$data['options']['links_sponsored'] = $rel == 3;
}
}

$this->media->update($data);
$this->media->options->update($data['options']);
}
and field structure
Grid::make()
->schema([
Select::make('rels')
->label(__('Tipos de enlaces'))
->helperText(__('Tipos de enlaces que permites en tus artículos'))
->multiple()
->options([
1 => 'Follow',
2 => 'NoFollow',
3 => 'Sponsored'
])
->required(),
])
Grid::make()
->schema([
Select::make('rels')
->label(__('Tipos de enlaces'))
->helperText(__('Tipos de enlaces que permites en tus artículos'))
->multiple()
->options([
1 => 'Follow',
2 => 'NoFollow',
3 => 'Sponsored'
])
->required(),
])
LeandroFerreira
LeandroFerreira16mo ago
select multiple should be ->default([2])

Did you find this page helpful?