For some reason the selected_types
select field in a blueprints file won’t save to the config file. This is part of the blueprints file:
media_types:
type: list
label: Media Types
fields:
.name:
type: text
label: Name
validate:
required: true
.selected_types:
type: select
size: long
classes: fancy
label: File types
help: 'Select file types belonging to this group'
multiple: true
default: jpg
options:
jpg: 'jpg'
jpeg: 'jpeg'
png: 'png'
When saved selected_types
appears to be saved as an empty string:
media_types:
-
name: Images
selected_types: ''
I must be overlooking something but I can’t figure out what.
@bleutzinn, If you are overlooking something, then so am I…
Some observations:
- The select field works fine when you comment out ‘multiple’
- When replacing it with a ‘selectize’ field, it also works.
My guess is there is something wrong with the field itself. Also see this issue at Github.
Thanks for the investigation.
I managed to track the problem down to the Grav core, in the ‘filterArray’ function in /system/src/Grav/Common/Data/BlueprintSchema.php line 170 to be specific.
When I do a dump($data)
before this function is called (at line 87 of the same php file) the field is still an array. After the ‘filterArray’ function it has suddenly become an empty string.
The ‘filterArray’ function performs field validation and uses recursion to handle nested fields. This is where things go wrong. What exactly remains a mystery to me though.
@bleutzinn, Did some debugging too…
When going down the rabit hole to Validation::filter()
…
That method determines how the field should be validated. In your case the method is set to ‘filterText’. ‘filterText’ will however return null because the value is not a string nor numeric.
When you add the following to your field definition:
validate:
type: array
Validation:filter()
will select method Validation::filterArray()
and the data is saved correctly.
“Is this documented somewhere?”, you may ask. Well… I found this remark in a total different field type Pages Field:
If you set multiple
to true, you need to add validate.type: array
. Otherwise the array of selected pages will not be saved correctly.
You could return the favour by creating a pull request with above remark for the select field.
Thank you for digging into this and providing a solution. I tested with validate.type: array
and indeed it’s working. What a great relief!
Of course I will make a PR towards the documentation. Again thanks!
PS a PR is currently pending.