Answers.Body: <p>With the help of <a href="https://craftcms.stackexchange.com/users/193/createsean">CreateSean</a> comment I have managed to fix the problems in the form.</p>
  <p>Here it is for future reference for everyone:</p>
  <pre><code>&lt;form method=&quot;post&quot; accept-charset=&quot;UTF-8&quot; enctype=&quot;multipart/form-data&quot; class=&quot;edit-store&quot;&gt;
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ redirectInput('members-zone/{slug}') }}
      {{ hiddenInput('movementCoursesEntries', '2') }}
      {{ hiddenInput('enabled', '1') }}
      &lt;input type=&quot;hidden&quot; name=&quot;entryId&quot; value=&quot;{{ entry.id }}&quot;&gt;
  
      {# various includes here #}
      &lt;input type=&quot;hidden&quot; name=&quot;fields[blocks]&quot; /&gt;
  
      {% for block in entry.videos %}
          {% if block.type == 'video_box' %}
              &lt;div class=&quot;matrixblock &quot;&gt;
                  &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][{{ block.id }}][type]&quot; value=&quot;video_box&quot;&gt;
                  &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][{{ block.id }}][enabled]&quot; value=&quot;1&quot;&gt;
                  &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][{{ block.id }}][fields][videoTitle]&quot; value=&quot;{{ block.videoTitle }}&quot;&gt;
                  &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][{{ block.id }}][fields][videoUrl]&quot; value=&quot;{{ block.videoUrl }}&quot;&gt;
              &lt;/div&gt;
          {% endif %}
      {% endfor %}
      {# hidden field with block type that is being updated #}
      &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][new_0][type]&quot; value=&quot;video_box&quot;&gt;
      {# is this block enabled #}
      &lt;input type=&quot;hidden&quot; name=&quot;fields[videos][new_0][enabled]&quot; value=&quot;1&quot;&gt;
      &lt;input type=&quot;text&quot; name=&quot;fields[videos][new_0][fields][videoTitle]&quot; value=&quot;&quot; placeholder=&quot;Video Title&quot;&gt;
      &lt;input type=&quot;text&quot; name=&quot;fields[videos][new_0][fields][videoUrl]&quot; value=&quot;&quot;  placeholder=&quot;Videl URL&quot;&gt;
      
      &lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;btn btn-primary&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-15T18:39:02.207Z
Answers.LastActivityDate: 2020-08-15T18:39:02.207Z
Answers.UserDisplayName: LessMore Creative Studio
Body: <p>I am trying to add a form to the frontend in which a registered user will be able to create and publish new block in a matrix field. I have read the documentation about it but am still failing miserably in achieving it...</p>
  <p>The entry is part of a structure section with sectionId <code>movementCoursesEntries</code>. The matrix fieldId is <code>videos</code>. The blockId is <code>video_box</code> and it has two plain text fields <code>videoTitle</code> and <code>videoUrl</code>.</p>
  <p>I have used this code but what I am getting is that the after submitting the form, all the old matrix blocks are being deleted and the new one is not being created. Any advice is highly appreciated!</p>
  <pre><code>&lt;form method=&quot;post&quot; accept-charset=&quot;UTF-8&quot;&gt;
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ redirectInput('/members-zone/{slug}') }}
      {{ hiddenInput('movementCoursesEntries', '2') }}
      {{ hiddenInput('enabled', '1') }}
      {{ hiddenInput('entryId', entry.id) }}
    
      {% if entry is defined %}
          {# Retain existing blocks + sort order #}
          {% for video_box in clone(entry.videos).anyStatus().ids() %}
              {{ hiddenInput('fields[videoTitle][sortOrder][]', video_box) }}
              {{ hiddenInput('fields[videoUrl][sortOrder][]', video_box) }}
          {% endfor %}
      {% endif %}
  
      {# Add a new text block #}
      {{ hiddenInput('fields[videoTitle][sortOrder][]', 'new:1') }}
      {{ hiddenInput('fields[videoUrl][sortOrder][]', 'new:1') }}
  
      {# Prefix the block's input names with `fields[&lt;FieldHandle&gt;][blocks][new:1]` #}
      {% namespace &quot;fields[blocks][blocks][new:1]&quot; %}
          {{ hiddenInput('type', 'text') }}
          &lt;input name=&quot;fields[videoTitle]&quot;&gt;
          &lt;input name=&quot;fields[videoUrl]&quot;&gt;
      {% endnamespace %}
  
    &lt;input type=&quot;submit&quot; value=&quot;Publish&quot;&gt;
  &lt;/form&gt;
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-14T21:13:27.787Z
Comments.Text: This blog post for craft 2 should help you get it working. You'll need to adapt for the changes from C2 to C3 https://caffeinecreations.ca/blog/editing-a-front-end-form-with-matrix/
Comments.UserDisplayName: CreateSean
Tags: <entries><matrix><frontend-form>
Title: adding a block of matrix field from a front end entry form
ThreadText: # adding a block of matrix field from a front end entry form
  
  (Asked by: username_0 on 2020-08-14)
  
  I am trying to add a form to the frontend in which a registered user will be able to create and publish new block in a matrix field. I have read the documentation about it but am still failing miserably in achieving it...
  
  The entry is part of a structure section with sectionId `movementCoursesEntries`. The matrix fieldId is `videos`. The blockId is `video_box` and it has two plain text fields `videoTitle` and `videoUrl`.
  
  I have used this code but what I am getting is that the after submitting the form, all the old matrix blocks are being deleted and the new one is not being created. Any advice is highly appreciated!
  
  ```
  <form method="post" accept-charset="UTF-8">
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ redirectInput('/members-zone/{slug}') }}
      {{ hiddenInput('movementCoursesEntries', '2') }}
      {{ hiddenInput('enabled', '1') }}
      {{ hiddenInput('entryId', entry.id) }}
    
      {% if entry is defined %}
          {# Retain existing blocks + sort order #}
          {% for video_box in clone(entry.videos).anyStatus().ids() %}
              {{ hiddenInput('fields[videoTitle][sortOrder][]', video_box) }}
              {{ hiddenInput('fields[videoUrl][sortOrder][]', video_box) }}
          {% endfor %}
      {% endif %}
  
      {# Add a new text block #}
      {{ hiddenInput('fields[videoTitle][sortOrder][]', 'new:1') }}
      {{ hiddenInput('fields[videoUrl][sortOrder][]', 'new:1') }}
  
      {# Prefix the block's input names with `fields[<FieldHandle>][blocks][new:1]` #}
      {% namespace "fields[blocks][blocks][new:1]" %}
          {{ hiddenInput('type', 'text') }}
          <input name="fields[videoTitle]">
          <input name="fields[videoUrl]">
      {% endnamespace %}
  
    <input type="submit" value="Publish">
  </form>
  
  ```
  
  **Tags:** entries, matrix, frontend-form
  
  > **Comments:**
  > **username_1**: This blog post for craft 2 should help you get it working. You'll need to adapt for the changes from C2 to C3 https://caffeinecreations.ca/blog/editing-a-front-end-form-with-matrix/  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-08-15. Score: 2 | Accepted answer
  
  With the help of [username_1](https://craftcms.stackexchange.com/users/193/username_1) comment I have managed to fix the problems in the form.
  
  Here it is for future reference for everyone:
  
  ```
  <form method="post" accept-charset="UTF-8" enctype="multipart/form-data" class="edit-store">
      {{ csrfInput() }}
      {{ actionInput('entries/save-entry') }}
      {{ redirectInput('members-zone/{slug}') }}
      {{ hiddenInput('movementCoursesEntries', '2') }}
      {{ hiddenInput('enabled', '1') }}
      <input type="hidden" name="entryId" value="{{ entry.id }}">
  
      {# various includes here #}
      <input type="hidden" name="fields[blocks]" />
  
      {% for block in entry.videos %}
          {% if block.type == 'video_box' %}
              <div class="matrixblock ">
                  <input type="hidden" name="fields[videos][{{ block.id }}][type]" value="video_box">
                  <input type="hidden" name="fields[videos][{{ block.id }}][enabled]" value="1">
                  <input type="hidden" name="fields[videos][{{ block.id }}][fields][videoTitle]" value="{{ block.videoTitle }}">
                  <input type="hidden" name="fields[videos][{{ block.id }}][fields][videoUrl]" value="{{ block.videoUrl }}">
              </div>
          {% endif %}
      {% endfor %}
      {# hidden field with block type that is being updated #}
      <input type="hidden" name="fields[videos][new_0][type]" value="video_box">
      {# is this block enabled #}
      <input type="hidden" name="fields[videos][new_0][enabled]" value="1">
      <input type="text" name="fields[videos][new_0][fields][videoTitle]" value="" placeholder="Video Title">
      <input type="text" name="fields[videos][new_0][fields][videoUrl]" value=""  placeholder="Videl URL">
      
      <input type="submit" value="Save" class="btn btn-primary">
  </form>
  
  ```
  
  
  ---
  
  