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

Error with yaml - Multiple Select form inside List form

Solved by Dimitri Longo View solution

Started by Francesco 9 years ago · 2 replies · 1235 views
9 years ago

Hi everyone!

Need some help with a grav error. I'm working on a gym website and I would like to create the classes schedule with 7 tabs (one for each day). In every tab there will be the classes of the selected day.

<details>
<summary>Example</summary>

Monday: yoga, pilates
Tuesday: pilates, box, karate
and so on...

</details>

So I created a class.yaml file with a list in order to add as many classes as I want. Inside the list, a Select form with Multiple enabled.

<details>
<summary>class.yaml file</summary>

header.classes:

YAML
          type: list
          label: Add Class
          fields:

            .title:
              type: text
              label: Name of the single class
            .days:
              type: select
              classes: fancy
              label: Days of classes
              multiple: true
              options:
                monday: Monday
                tuesday: Tuesday
                wednesday: Wednesday
                thursday: Thursday
                friday: Friday
                saturday: Saturday
                sunday: sunday

</details>

On back end it shows up good, but when I'm going to save something in this form I got this error "Array to string conversion"

<details>
<summary>Error Detail</summary>

if ((!isset($params['multiline']) || !$params['multiline']) && preg_match('/\R/um', $value)) {
return false;
}

PHP
    return true;
}

protected static function filterText($value, array $params, array $field)
{
    return (string) $value;
}

protected static function filterCommaList($value, array $params, array $field)
{
    return is_array($value) ? $value : preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
}

protected static function typeCommaList($value, array $params, array $field)
{
    return is_array($value) ? true : self::typeText($value, $params, $field);
}

protected static function filterLower($value, array $params)
{

</details>

Am I doing something wrong? How can I prevent this to happen?
Thanks for the help!

9 years ago Solution

try add validate commalist or array

YAML
  validate:
         type: commalist

or

YAML
validate:
          type: array
👍 1
9 years ago

Awesome! Adding validate: type: array worked, thanks dimitrilogo ;)

If I can, I would like to ask another thing about this. I want to generate the content of these tabs automatically.
Basically I'm trying to make every class with the content of the array .days to be posted inside the correct tab.

Example
I create a new item in the list form with Title: Yoga Class and Days: Monday, Friday. So I want that this Yoga Class is going to be posted in both tabs Monday and Friday. This using the yaml file I posted above.

<details>
<summary>Twig</summary>

<div id="classes_monday" class="tab">
{% for item in header.classes %}
{% if item.days == 'monday' %}
<ul>
<li>{{ item.title }}</li>
<li>{{ item.days }}</li>
</ul>
{% endif %}
{% endfor %}
</div>

</details>

EDIT:

ok I found it, If someone need to do the same I used {% if ''monday' in item.days %}

<details>
<summary>Code</summary>

<div id="classes_monday" class="tab">
{% for item in header.classes %}
{% if ''monday' in item.days %}
<ul>
<li>{{ item.title }}</li>
<li>{{ item.days }}</li>
</ul>
{% endif %}
{% endfor %}
</div>

</details>

Thanks again dimitrilogo for your help!

last edited 08/22/17 by Francesco

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 60 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 132 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago