This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Could someone help me with the following problem:
I have a dropdown in a backend form which nicely displays the options and saves the selection to datatabase. But when I Save&Close and then re-open the record to edit it, the dropdown does't display the saved option, but shortly displays my emptyOption
So the form doesn't get the saved option from the database. What may I be missing?
Thank you
Here is my code:
fields.yml:
biketype:
label: 'Bike Type'
span: auto
emptyOption: '<please choose>'
type: dropdown
In the model:
public function getBiketypeOptions()
{
return ['all' => e(trans('cdrr.bicycles::lang.bicycles.all')),
'mtb' =>e(trans('cdrr.bicycles::lang.bicycles.mtb')),
'race' => e(trans('cdrr.bicycles::lang.bicycles.race')),
'cross' =>e(trans('cdrr.bicycles::lang.bicycles.cross')),
'tra' => e(trans('cdrr.bicycles::lang.bicycles.tra')),
'city' =>e(trans('cdrr.bicycles::lang.bicycles.cty')),
'bmx' =>e(trans('cdrr.bicycles::lang.bicycles.bmx')),
'fat' => e(trans('cdrr.bicycles::lang.bicycles.fat')),
'bal' => e(trans('cdrr.bicycles::lang.bicycles.bal'))];
}
public function getBiketypeAttribute()
{
$value = array_get($this->attributes, 'biketype');
if ($value){
return array_get($this->getBiketypeOptions(), $value);
}
}
in the two lang files something like this:
<?php return [
'bicycles' => [
'all' => 'Allround',
'mtb' => 'mtb',
'race' => 'Race Bike',
'cross' => 'Cyclocross/Gravel',
'tra' => 'Track Bike',
'city'=> 'City',
'bmx' => 'BMX',
'fat' => 'Fat-Bike',
'bal' => 'Balance Bike'
],
];
?>
It behaves the same with both backend languages I have.
Thanks again for any hints
Last updated
Just solved it. I ran into the same trap as @manuel.bua here: https://octobercms.com/forum/post/populate-dropdown-selected-field-value-on-edit-page
For me it was not about sortable columns, but the need to display the attribute values in frontend.
So I didn't have to change anything in columns.yml. I only use a separate function for the use in frontend now:
public function getBiketypeAttribute()
{
$key = array_get($this->attributes, 'biketype');
return $key;
}
public function getBiketypeFrontendAttribute()
{
$value = array_get($this->attributes, 'biketype');
if ($value){
return array_get($this->getBiketypeOptions(), $value);
}
}
works :)
1-2 of 2