Answers.Body: <p><a href="https://craftcms.com/docs/4.x/matrix-fields.html#saving-matrix-fields" rel="nofollow noreferrer">As per the documentation</a>, you need to include the <code>sortOrder</code> parameter to save multiple blocks. Include three hidden fields to pass the three new block IDs (<code>new1</code>, <code>new2</code>, <code>new3</code>) as a <code>sortOrder</code> array and the field should save successfully.</p>
  <pre class="lang-twig prettyprint-override"><code>&lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][sortOrder][]&quot; value=&quot;new1&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][sortOrder][]&quot; value=&quot;new2&quot;&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][sortOrder][]&quot; value=&quot;new3&quot;&gt;
  </code></pre>
  <p>By the way, this line is not doing anything and <em>might</em> mess something up:</p>
  <pre class="lang-twig prettyprint-override"><code>&lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement]&quot;/&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-02T07:17:31.753Z
Answers.Text: Hi, thanks for your answer. I have added the code which is not showing any fields when a user completest the form.
Answers.UserDisplayName: aren
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-02T07:23:54.960Z
Answers.Text: @aren Make sure to check the `network` tab in your devtools to see if the request contains the correct payload and what response you're getting. Also, have you checked if the users in question have the required permission to update this entry? Check the logs as well to see if you're getting any errors.
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-02T07:52:40.903Z
Answers.Text: I'm not getting any errors, I have assigned all the rights to the user to test but in the logs I don't find the field either.
Answers.UserDisplayName: aren
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-02T08:16:50.777Z
Answers.Text: Okay I got it somehow working by adding this line (<input type="hidden" name="fields[deviceManagement]"/>) only one
Answers.UserDisplayName: aren
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-20T13:27:23.457Z
Answers.LastActivityDate: 2022-07-20T13:27:23.457Z
Answers.UserDisplayName: MoritzLost
Body: <p>I am using a matrix field for registered users on my site.
  I want to be able to add a certain amount of matrix blocks to each users on registration (with hidden input), so I can re-use them later on.</p>
  <p>For now this kinda works, when a user registers only one block is added.</p>
  <p>My question: How can I add the other matrix blocks to the user.</p>
  <p>Used this code below (which is inside the form)(hidden-devices.twig):</p>
  <pre><code>&lt;div class=&quot;matrixblock&quot; data-id=&quot;new1&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement]&quot;/&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new1][type]&quot; value=&quot;device&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new1][enabled]&quot; value=&quot;1&quot;&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new1][fields][macadres]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new1][fields][serial]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new1][fields][webkey]&quot; value=&quot;&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;matrixblock&quot; data-id=&quot;new2&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement]&quot;/&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new2][type]&quot; value=&quot;device&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new2][enabled]&quot; value=&quot;1&quot;&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new2][fields][macadres]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new2][fields][serial]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new2][fields][webkey]&quot; value=&quot;&quot;&gt;
      &lt;/div&gt;
      &lt;div class=&quot;matrixblock&quot; data-id=&quot;new3&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement]&quot;/&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new3][type]&quot; value=&quot;device&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new3][enabled]&quot; value=&quot;1&quot;&gt;
  
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new3][fields][macadres]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new3][fields][serial]&quot; value=&quot;&quot;&gt;
            &lt;input type=&quot;hidden&quot; name=&quot;fields[deviceManagement][new3][fields][webkey]&quot; value=&quot;&quot;&gt;
      &lt;/div&gt;
  </code></pre>
  <p>the form(register.twig):</p>
  <pre><code>&lt;form class=&quot;c-forms&quot; method=&quot;post&quot; accept-charset=&quot;UTF-8&quot; id=&quot;19898981&quot;&gt;
          {% set page = craft.entries.section('login').one() %}
          {{ csrfInput() }}
          {{ actionInput('users/save-user') }}
          {{ redirectInput(page.url) }}
          {% include &quot;_components/hidden-devices.twig&quot; %}
  
          &lt;div class=&quot;container&quot;&gt;
            &lt;div class=&quot;row&quot;&gt;
            
              &lt;div class=&quot;col-lg-4 col-md-6 col-sm-12 col-xs-12 col-xl-4 col-xxl-4&quot;&gt;
                &lt;label class=&quot;c-forms__label&quot; for=&quot;firstName&quot;&gt;{{ entry.registerVoornaamLabel }}&lt;/label&gt;
                &lt;input class=&quot;c-forms__input&quot; class=&quot;form-control&quot; type=&quot;text&quot; id=&quot;firstName&quot; name=&quot;firstName&quot; placeholder=&quot;{{ entry.registerVoornaamPlaceholder }}&quot; autocomplete=&quot;given-name&quot; required&gt;
                {{ user ? _self.errorList(user.getErrors('firstName')) }}
  
                &lt;label class=&quot;c-forms__label&quot; for=&quot;bedrijf&quot;&gt;{{ entry.registerCompanyLabel }}&lt;/label&gt;
                &lt;input class=&quot;c-forms__input&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;bedrijf&quot; name=&quot;fields[bedrijf]&quot; placeholder=&quot;{{ entry.registerCompanyPlaceholder }}&quot; required&gt;
                {{ user ? _self.errorList(user.getErrors('fields[bedrijf]')) }}
  
                &lt;label class=&quot;c-forms__label&quot; for=&quot;btw&quot;&gt;{{ entry.registerBtwLabel }}&lt;/label&gt;
                &lt;input class=&quot;c-forms__input&quot; type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;btw&quot; name=&quot;fields[btw]&quot; placeholder=&quot;{{ entry.registerBtwPlaceholder }}&quot; required&gt;
                {{ user ? _self.errorList(user.getErrors('fields[btw]')) }}
  
                
  
              &lt;/div&gt;
         &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  
  </code></pre>
  
Tags: <matrix><form><field><blocks>
Title: Adding multiple blocks for matrix in form
ThreadText: # Adding multiple blocks for matrix in form
  
  (Asked by: username_0 on 2022-07-20)
  
  I am using a matrix field for registered users on my site.
  I want to be able to add a certain amount of matrix blocks to each users on registration (with hidden input), so I can re-use them later on.
  
  For now this kinda works, when a user registers only one block is added.
  
  My question: How can I add the other matrix blocks to the user.
  
  Used this code below (which is inside the form)(hidden-devices.twig):
  
  ```
  <div class="matrixblock" data-id="new1">
            <input type="hidden" name="fields[deviceManagement]"/>
  
            <input type="hidden" name="fields[deviceManagement][new1][type]" value="device">
            <input type="hidden" name="fields[deviceManagement][new1][enabled]" value="1">
  
            <input type="hidden" name="fields[deviceManagement][new1][fields][macadres]" value="">
            <input type="hidden" name="fields[deviceManagement][new1][fields][serial]" value="">
            <input type="hidden" name="fields[deviceManagement][new1][fields][webkey]" value="">
      </div>
      <div class="matrixblock" data-id="new2">
            <input type="hidden" name="fields[deviceManagement]"/>
  
            <input type="hidden" name="fields[deviceManagement][new2][type]" value="device">
            <input type="hidden" name="fields[deviceManagement][new2][enabled]" value="1">
  
            <input type="hidden" name="fields[deviceManagement][new2][fields][macadres]" value="">
            <input type="hidden" name="fields[deviceManagement][new2][fields][serial]" value="">
            <input type="hidden" name="fields[deviceManagement][new2][fields][webkey]" value="">
      </div>
      <div class="matrixblock" data-id="new3">
            <input type="hidden" name="fields[deviceManagement]"/>
  
            <input type="hidden" name="fields[deviceManagement][new3][type]" value="device">
            <input type="hidden" name="fields[deviceManagement][new3][enabled]" value="1">
  
            <input type="hidden" name="fields[deviceManagement][new3][fields][macadres]" value="">
            <input type="hidden" name="fields[deviceManagement][new3][fields][serial]" value="">
            <input type="hidden" name="fields[deviceManagement][new3][fields][webkey]" value="">
      </div>
  
  ```
  
  the form(register.twig):
  
  ```
  <form class="c-forms" method="post" accept-charset="UTF-8" id="19898981">
          {% set page = craft.entries.section('login').one() %}
          {{ csrfInput() }}
          {{ actionInput('users/save-user') }}
          {{ redirectInput(page.url) }}
          {% include "_components/hidden-devices.twig" %}
  
          <div class="container">
            <div class="row">
            
              <div class="col-lg-4 col-md-6 col-sm-12 col-xs-12 col-xl-4 col-xxl-4">
                <label class="c-forms__label" for="firstName">{{ entry.registerVoornaamLabel }}</label>
                <input class="c-forms__input" class="form-control" type="text" id="firstName" name="firstName" placeholder="{{ entry.registerVoornaamPlaceholder }}" autocomplete="given-name" required>
                {{ user ? _self.errorList(user.getErrors('firstName')) }}
  
                <label class="c-forms__label" for="bedrijf">{{ entry.registerCompanyLabel }}</label>
                <input class="c-forms__input" type="text" class="form-control" id="bedrijf" name="fields[bedrijf]" placeholder="{{ entry.registerCompanyPlaceholder }}" required>
                {{ user ? _self.errorList(user.getErrors('fields[bedrijf]')) }}
  
                <label class="c-forms__label" for="btw">{{ entry.registerBtwLabel }}</label>
                <input class="c-forms__input" type="text" class="form-control" id="btw" name="fields[btw]" placeholder="{{ entry.registerBtwPlaceholder }}" required>
                {{ user ? _self.errorList(user.getErrors('fields[btw]')) }}
  
                
  
              </div>
         </div>
      </div>
  </form>
  
  ```
  
  **Tags:** matrix, form, field, blocks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-07-20. Score: 0 | Accepted answer
  
  [As per the documentation](https://craftcms.com/docs/4.x/matrix-fields.html#saving-matrix-fields), you need to include the `sortOrder` parameter to save multiple blocks. Include three hidden fields to pass the three new block IDs (`new1`, `new2`, `new3`) as a `sortOrder` array and the field should save successfully.
  
  ```twig
  <input type="hidden" name="fields[deviceManagement][sortOrder][]" value="new1">
  <input type="hidden" name="fields[deviceManagement][sortOrder][]" value="new2">
  <input type="hidden" name="fields[deviceManagement][sortOrder][]" value="new3">
  
  ```
  
  By the way, this line is not doing anything and *might* mess something up:
  
  ```twig
  <input type="hidden" name="fields[deviceManagement]"/>
  
  ```
  
  > **Comments:**
  > **username_0**: Hi, thanks for your answer. I have added the code which is not showing any fields when a user completest the form.  
  > **username_1**: @username_0 Make sure to check the `network` tab in your devtools to see if the request contains the correct payload and what response you're getting. Also, have you checked if the users in question have the required permission to update this entry? Check the logs as well to see if you're getting any errors.  
  > **username_0**: I'm not getting any errors, I have assigned all the rights to the user to test but in the logs I don't find the field either.  
  > **username_0**: Okay I got it somehow working by adding this line (<input type="hidden" name="fields[deviceManagement]"/>) only one  
  
  ---
  
  