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

Dynamic Select field in Frontend Form

Started by Muut Archive 10 years ago · 8 replies · 1805 views
10 years ago

Hey there,

I'm trying to fill a select field with dynamic options, like pages from a certain subfolder, but it does not seem to work in frontend forms.

YAML
form:
  name: contact
  fields: 
    - name: Name
      type: text
      label: "Your Name"
    - name: Recipient
      type: select
      label: "send to"
      '@data-options': '\Grav\Theme\MyOwnTheme::getRecipients'

So is there a way to populate a select with dynamic options? Or is it possible to create a complete custom Form-Field-Type?

10 years ago

What version of Grav are you using? If I recall correctly, Twig variables will be available in FrontMatter in 1.1 (which is in Beta currently).

10 years ago

Ok, sounds good! But I'm using version 1.0.10 and a Beta is not an option for this project. Think I'm going to write a for this if there is no other way.

10 years ago

That's exactly what I have tried, but it does not seem to work in the frontend with the form plugin. I even tried to use that example and added the following to my form frontmatter in the form.md file

YAML
form:
  name: 'MyForm'
  fields:
    ...
    - name: timezone
      type: select
      label: PLUGIN_ADMIN.TIMEZONE
      size: medium
      classes: fancy
      help: PLUGIN_ADMIN.TIMEZONE_HELP
      data-options@: '\Grav\Common\Utils::timezones'
      default: ''
      options:
        '': 'Default (Server Timezone)'
    ...

The result is, that the select input is shown, but only has one option: 'Default (Server Timezone)'
Also the label is not shown, and i tried to use the key 'data-options@', as in your given example, as well as '@data-options' li in my systems system.yaml

So I guess, this does not work in frontend forms. Can anybody reproduce that behavior?

10 years ago

I have Grav 1.1.3 and can't get a select field to dynamically populate from my theme.

---yaml
data-options@: '\Grav\Theme\MyGravTheme::getElementSizeOptions'

JS

---php
public static function getElementSizeOptions() {
    $sizes = [
      'full' => 'Full Width',
      'eighty' => '80%',
      'sixty' => '60%',
      'half' => '50%',
      'forty' => '40%'
      ];
    return $sizes;
  }
---
10 years ago

The frontend theme class is probably not loaded in the admin (it is in the frontend). Can you put this in a plugin instead?

10 years ago

I have the same problem as timbo in a front-end form, can't populate even from


data-options@: '\Grav\Common\Utils::timezones'

TXT
Also tried from my own plugin, doesn't populate either

data-options@: '\Grav\Plugin\MyPlugin::myutil'

TXT


Just updated to last version of Grav. Is there another way of doing this?
10 years ago

I finally solved this by writing a simple Plugin, so build the form "by-hand" and so populate the select boxes direct in the twig template.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1360 9 years ago
Archive · by Muut Archive, 9 years ago
2 938 9 years ago
Archive · by Muut Archive, 9 years ago
2 4068 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1122 9 years ago