Hi guys,
I’m fetching database data and adding it to a select field:
Database data
- Table: accommodatie_status
- Column 1: id (int)
- Column 2: status_nl (text)
Page model
public function getZoekertjesStatus():array {
$statuses = Db::select("accommodatie_status", ["id", "status_nl"]);
$array = [];
if( !$statuses ) {
return $array;
}
foreach( $statuses as $item ) {
$array[$item->id()] = $item->status_nl();
}
return $array;
}
Page blueprint
accommodatie_status_id:
label: Status
type: select
options:
type: query
query: site.find('zoekertjes').getZoekertjesStatus
value: '{{ arrayItem.key }}'
text: "{{ arrayItem.value }}"
When changing the select field value, the database gets updated correctly. However, the select field doesn’t stay selected on the current value, it alows shows the empty value:
When I change the select field to a text field, the value is getting represented correctly:
I would like the select field to keep the current database value selected.
Is there something I’m missing here? Thanks!
Maybe a problem with data types (string vs. int).
1 Like
I would like to revive this old thread:
On my website (Kirby 5.1.4), I generate virtual user pages from the data of registered users.
Here, I encounter exactly the same problem as the TO:
When I display the virtual user pages in the panel, select field values are not selected. If I change the field type to text, the values contained are displayed correctly. When I display the user profiles with exactly the same select fields in the panel, the data is selected correctly. I have searched through all the documentation and the forum. This thread is the only one that mentions such behaviour.
Any ideas?
TIA
Thomas
I found a solution by following @pixelijn’s tip regarding ‘data type’:
This is an excerpt from my model file:
'memberCountryGroupCode' => $user->memberCountryGroupCode(),
'memberAreaCode' => $user->memberAreaCode(),
'memberGrade' => $user->memberGrade(),
With this, the problem described above existed. After explicitly converting to strings.
'memberCountryGroupCode' => $user->memberCountryGroupCode()->toString(),
'memberAreaCode' => $user->memberAreaCode()->toString(),
'memberGrade' => $user->memberGrade()->toString(),
The problem was solved and the correct select options are displayed.
I now wonder what was originally output if not strings?
Field objects
When you create arrays and need a string value, always call value() or toString()
1 Like
Thank you very much, Sonja – once again.