getgrav / grav-plugin-form

Grav Form Plugin
http://getgrav.org
MIT License
53 stars 79 forks source link

/templates/forms/fields/select/select.html.twig doesn't load iterable options properly #557

Closed jgonyea closed 2 years ago

jgonyea commented 2 years ago

Using the following blueprint:

    my_field:
      type: select
      classes: fancy
      options:
        "option1":
          value: Option 1
        "option2":
          value: Option 2
        "option3":
          disabled: true
          value: My Disabled Option
      default: option1

I believe the select field twig has two bugs in it when the options are iterable (see above).

Bug1: Saved data not loaded into form on page refresh.

The first is at L46:

{{ item_value.selected ? 'selected="selected"' : '' }}

This will not properly load/ select the iterable stored data. To fix, I believe the line should look like:

{{ item_value.selected or key == value ? 'selected="selected"' : '' }}

This will take into consideration a manually selected option as well as when the option is set from loaded data.

Bug2: Option key is not stored properly

The second related bug is at L48:

value="{{ item_value.akey }}"

akey is calculated at L43, but isn't referenced properly. I believe the L48 line should read:

value="{{ akey }}"

Changing L48 will set the proper key in a form based on the key in the options array.

jgonyea commented 2 years ago

PR submitted: https://github.com/getgrav/grav-plugin-form/pull/558