0

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>

1 Answer 1

0

As per the documentation, 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.

<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:

<input type="hidden" name="fields[deviceManagement]"/>
4
  • Hi, thanks for your answer. I have added the code which is not showing any fields when a user completest the form.
    – aren
    Commented Aug 2, 2022 at 7:17
  • @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.
    – MoritzLost
    Commented Aug 2, 2022 at 7:23
  • 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.
    – aren
    Commented Aug 2, 2022 at 7:52
  • Okay I got it somehow working by adding this line (<input type="hidden" name="fields[deviceManagement]"/>) only one
    – aren
    Commented Aug 2, 2022 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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