Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

Problems with select field

Solved by pamtbaau View solution

Started by Ron Wardenier 6 years ago · 4 replies · 913 views
6 years ago

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:

YAML
        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:

YAML
media_types:
  -
    name: Images
    selected_types: ''

I must be overlooking something but I can't figure out what.

6 years ago

@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.

6 years ago

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.

6 years ago Solution

@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:

YAML
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. ;-)

last edited 07/04/20 by pamtbaau
6 years ago

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.

last edited 07/04/20 by Ron Wardenier

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1165 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 89 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 163 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 138 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 156 7 months ago