0

A tricky one.

I have a form which sits on a section entry template. When user submits form from one page of the template, it saves the entries into the table.

BUT user going to another page of the template (where user can see same form), the table doesn't keep the previously stored instruction ID rows. See first column, only saving from most recent form submit.

Ultimately I'm just trying to store the checkbox value (1 inside each matrix block). In table, if block.id exists (in 'instruction' column), then overwrite checkbox value, otherwise add new row.

enter image description here

How the tutorial template is set up:

template for tutorial

form which wraps around the matrix blocks

matrix block contains form input checkbox

submit sends form data to entry of user in another Section (see below)

The makeshift "database" Section to store values:

Section

Entry per user

Inside Entry I have a table (multiple per tutorial parent)

"instruction" column holds the block.id and "done" holds value of checkbox.

enter image description here

In the crazy if/for logic around the checkbox input, I had to do this to get it to work, where if a table had rows, it wasn't showing checkboxes on other tutorials, etc, etc.

    {% set progressBob = craft.entries.section('userProgress').authorId(currentUser.id).find() %}

    <form method="post" accept-charset="UTF-8">
      {{ getCsrfInput() }}
      <input type="hidden" name="action" value="entries/saveEntry">
      <input type="hidden" name="redirect" value="">
      <input type="hidden" name="sectionId" value="6">
      <input type="hidden" name="enabled" value="1">

      {% macro errorList(errors) %}
          {% if errors %}
              <ul class="errors">
                  {% for error in errors %}
                      <li>{{ error }}</li>
                  {% endfor %}
              </ul>
          {% endif %}
      {% endmacro %}

      {% from _self import errorList %}

      {% for entry in progressBob %}
        {% set tableentry = entry %}
        <input type="hidden" name="entryId" value="{{ tableentry.id }}">
      {% endfor %}

      {% for block in entry.tutorialMatrix %}
        {% if block.type == "instruction" %}

            {% set tableProgress = product.slug|trim('kit') ~ 'Progress' %}

            <input type="hidden" id="instruction{{ block.id }}" name="fields[{{ tableProgress }}][{{ block.id }}][col1]" value="{{ block.id }}" />

            <div class="instruction m-b-3 m-l-3">
              <label class="checkbox">

              {% if progressBob %} 

                {% for entry in progressBob %}
                  {% set tableentry = entry %}
                  {% if tableentry[tableProgress] %}

                      {% for row in tableentry[tableProgress] %} 

                          {% if row.instruction == block.id %}
                            <input type="hidden" id="done{{ block.id }}" value="" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" class="option-input checkbox" />
                            <input type="checkbox" id="done{{ block.id }}" value="1" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" {% if row.done %} checked="checked"{% endif %} class="option-input checkbox" />
                          {% endif %}

                      {% endfor %}

                  {% else %}
                    <input type="hidden" id="done{{ block.id }}" value="" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" class="option-input checkbox" />
                    <input type="checkbox" id="done{{ block.id }}" value="1" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" {% if row.done %} checked="checked"{% endif %} class="option-input checkbox" />
                  {% endif %}
                {% endfor %}

              {% else %} 
                <input type="hidden" id="done{{ block.id }}" value="" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" class="option-input checkbox" />
                <input type="checkbox" id="done{{ block.id }}" value="1" name="fields[{{ tableProgress }}][{{ block.id }}][col2]" {% if row.done %} checked="checked"{% endif %} class="option-input checkbox" />
              {% endif %}

              <span class="instructionLabel">{{ block.text }}</span>
              <pre class="prettyprint linenums">{{ block.code }}</pre>
              {% set image = block.image.first() %}
              {% if image %}
                <img src="{{ image.getUrl() }}" class="content-image" alt="{{ image.title }}">
              {% endif %}
              </label>
            </div>  

        {% endif %}
      {% endfor %}

      <input type="submit" value="Publish" class="btn btn-primary">

    </form>

Any help is much appreciated

| improve this question | | | | |
2

You can't just append rows to an existing table field from a form. The controller expects the complete data set, so you'd need to query for the existing rows in your template and post them together with the new rows for that field.

| improve this answer | | | | |
  • Thanks @carlcs, that makes sense. But this solution (or method I'm using to store data) doesn't feel right if to add 1 row, it has to re-submit the whole table which could be hundreds of rows. Or do you think this is OK? Or should I find another method? – Rich Holt Mar 18 '16 at 9:26
  • I could create a table for every tutorial, so the table just stores the checkbox value. But this would be painful – Rich Holt Mar 18 '16 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.