<p>The old blocks are likely being discarded due to the way Craft handles “delta saves” for complex fields like Matrix.</p>
<p><a href="https://craftcms.com/docs/4.x/matrix-fields.html#block-order" rel="nofollow noreferrer">This section</a> of the Matrix field documentation should help you persist existing blocks!</p>
<pre><code>{# Output existing block IDs to keep them around: #}
{% for blockId in entry.questions.anyStatus().ids() %}
  {{ hiddenInput('fields[questions][sortOrder][]', blockId) }}
{% endfor %}

{# Tell Craft about the new block: #}
{{ hiddenInput('fields[questions][sortOrder][]', 'new:1') }}

{# Output your fields, as you are now! #}
</code></pre>
<p>You do <em>not</em> need to POST all the existing fields back to persist the blocks—as long as their IDs are in the <code>sortOrder</code> array, Craft will leave them as-is!</p>
<blockquote>
<p> You can also insert the block at the beginning by placing the <code>new:1</code> input <em>before</em> the existing block IDs.</p>
</blockquote>

CC BY-SA 4.0
2023-08-24T00:04:52.943Z
2023-08-24T00:04:52.943Z
August Miller
<p>I have this in my entry form:</p>
<pre><code>        {{ csrfInput() }}
        {{ actionInput('entries/save-entry') }}
        {{ hiddenInput('entryId', entry.id) }}
        {{ hiddenInput('fields[questions][sortOrder][]', 'new:1') }}
        {{ hiddenInput('sectionId', section.id) }}
        {% namespace &quot;fields[questions][blocks][new:1]&quot; %}
        {{ hiddenInput('type', 'question') }}
        &lt;div&gt;
            &lt;label for=&quot;question&quot;&gt;Question&lt;/label&gt;
            &lt;input
                type=&quot;text&quot;
                name=&quot;fields[question]&quot;
                id=&quot;question&quot;
                value=&quot;…&quot;
            &gt;
        &lt;/div&gt;
        {% endnamespace %}
</code></pre>
<p>This is working fine for saving a new matrix block, but if I then want to resubmit and save more blocks, it's not creating new blocks, but overwriting the data of the existing one.</p>
<p>What do I need to change for it to save new blocks as well?</p>

<matrix><entry-form>
Entry form replacing rather than appending data to a matrix field
# Entry form replacing rather than appending data to a matrix field

(Asked by: username_0 on 2023-08-23)

I have this in my entry form:
        {{ csrfInput() }}
        {{ actionInput('entries/save-entry') }}
        {{ hiddenInput('entryId', entry.id) }}
        {{ hiddenInput('fields[questions][sortOrder][]', 'new:1') }}
        {{ hiddenInput('sectionId', section.id) }}
        {% namespace "fields[questions][blocks][new:1]" %}
        {{ hiddenInput('type', 'question') }}
        
            Question
            
        
        {% endnamespace %}

This is working fine for saving a new matrix block, but if I then want to resubmit and save more blocks, it's not creating new blocks, but overwriting the data of the existing one.
What do I need to change for it to save new blocks as well?

**Tags:** matrix, entry-form

---

## 1 answers

### Answer by username_1 on 2023-08-24. Score: 3 | Accepted answer

The old blocks are likely being discarded due to the way Craft handles “delta saves” for complex fields like Matrix.
This section of the Matrix field documentation should help you persist existing blocks!
{# Output existing block IDs to keep them around: #}
{% for blockId in entry.questions.anyStatus().ids() %}
  {{ hiddenInput('fields[questions][sortOrder][]', blockId) }}
{% endfor %}

{# Tell Craft about the new block: #}
{{ hiddenInput('fields[questions][sortOrder][]', 'new:1') }}

{# Output your fields, as you are now! #}

You do not need to POST all the existing fields back to persist the blocks—as long as their IDs are in the sortOrder array, Craft will leave them as-is!

 You can also insert the block at the beginning by placing the new:1 input before the existing block IDs.


---

