Answers.Body: <p>The issue was indeed related to a bug (if we can call it like this) which was fixed in Craft 4.3.7.</p>
  <p>Here some references:</p>
  <ul>
  <li>issue: <a href="https://github.com/craftcms/cms/issues/12283" rel="nofollow noreferrer">https://github.com/craftcms/cms/issues/12283</a></li>
  <li>pull request with bugfix: <a href="https://github.com/craftcms/cms/pull/12544" rel="nofollow noreferrer">https://github.com/craftcms/cms/pull/12544</a></li>
  </ul>
  <p>I tested the bugfix in my specific situation and it worked as I needed, so this can be considered fixed.</p>
  <p>I posted this answer mainly for anyone with an older version of craft having trouble trying to achieve the very same goal.</p>
  <p>Thanks to @MatsMikkelRummelhoff and The Craft Team for the help and for fixing this.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-29T09:03:43.717Z
Answers.LastActivityDate: 2023-03-29T09:03:43.717Z
Answers.UserDisplayName: Masiorama
Body: <p>I am trying to prepopulate a hidden field on frontend user registration from with another user element... to explain better:any client user could be connected to an agent user, if a parameter (agentId) is present in the query string on the registration form page.</p>
  <p>Looking at the backend part: I created the associatedAgent custom field, which has been added to user fields.</p>
  <p>I retrieve <em>agentId</em> and <em>associatedAgent</em> this way:</p>
  <pre><code>{% set associatedAgentId = craft.app.request.getParam('agentId') %} 
  {% set associatedAgent = craft.users().group('agents').id(associatedAgentId).one() %}   
  </code></pre>
  <p>In the form I am using an hiddenInput() with my custom field name <em>associatedAgent</em>. I tried with:</p>
  <pre><code>{{ hiddenInput('fields[associatedAgent]', associatedAgent.id) }}
  </code></pre>
  <p>and</p>
  <pre><code>{{ hiddenInput('fields[associatedAgent][]', associatedAgent.id) }}
  </code></pre>
  <p>but it seems that no data is passed...</p>
  <p>I also tried to look on the B and check how the user form handles that very custom field, selecting an agent by hand and looking at the payload on save, and this is what I see:</p>
  <p><a href="https://i.sstatic.net/53nZs.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/53nZs.jpg" alt="enter image description here" /></a></p>
  <p>No idea how to fix it. What am I doing wrong? Thanks!</p>
  <p>Edit:</p>
  <pre><code>{% set associatedAgentId = craft.app.request.getParam('agentId') %}
  {% set associatedAgent = craft.users().group('agents').id(associatedAgentId).one() %}
  
  &lt;form method=&quot;post&quot; action=&quot;&quot; accept-charset=&quot;UTF-8&quot;&gt;
    {{ csrfInput() }}
    {{ actionInput('users/save-user') }}
    {{ redirectInput(redirectUrl) }}
    {{ hiddenInput('fields[associatedAgent][]', associatedAgent ? associatedAgent.id : null) }}
  
    {% macro errorList(errors) %}
      {% if errors %}
        {{ ul(errors, { class: 'errors text-red-500' }) }}
      {% endif %}
    {% endmacro %}
  
    {% from _self import errorList %}
  
    &lt;div class=&quot;field mt-3&quot;&gt;&lt;/div&gt;
  
    &lt;div class=&quot;field mt-3&quot;&gt;
      &lt;label for=&quot;email&quot;&gt;{{- 'Email'|t -}}&lt;/label&gt;
      &lt;div class=&quot;mt-1&quot;&gt;
        {{ input('email', 'email', user.email ?? cart.email ?? null, { id: 'email', class: 'w-full border border-gray-300 hover:border-gray-500 px-4 py-2 leading-tight rounded' }) }}
        {% if user is defined %}
          {{ errorList(user.getErrors('email')) }}
        {% endif %}
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class=&quot;field mt-3&quot;&gt;
      &lt;label for=&quot;savePassword&quot;&gt;{{- 'Password'|t -}}&lt;/label&gt;
      &lt;div class=&quot;mt-1&quot;&gt;
        {{ input('password', 'password', null, { id: 'savePassword', class: 'w-full border border-gray-300 hover:border-gray-500 px-4 py-2 leading-tight rounded' }) }}
        {% if user is defined %}
          {{ errorList(user.getErrors('password')) }}
        {% endif %}
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class=&quot;mt-3 lg:flex lg:justify-between lg:items-center&quot;&gt;
      &lt;div&gt;{{ tag('button', { type: 'submit', class: 'cm-button', text: 'Create account'|t }) }}&lt;/div&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Here is the payload of the sent register form... the id is correctly sent, but it is not registered at all:</p>
  <p><a href="https://i.sstatic.net/cViu9.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/cViu9.jpg" alt="register form payload" /></a></p>
  <p>Update 2, <strong>Probably the issue is this one</strong>: I forgot to mention that when I associate the <em>associatedAgent</em> field to <em>user fields</em> settings I add a condition, as you can see below:</p>
  <p><a href="https://i.sstatic.net/ab7oO.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ab7oO.jpg" alt="Associated agent field to user fields settings" /></a></p>
  <p>This is supposed to show the select field only for users which are indeed <em>clients</em>, because I do not want panel admins to be able to associate agents to agents, or agents to admins... well, if this setting is configured as above, the saving fails. If I clear the condition shown above than the association is correctly saved... I guess this is a bug in craft then?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-03T20:33:02.403Z
Comments.Text: `{{ hiddenInput('fields[associatedAgent][]', associatedAgent.id) }}` is the correct way to do it, so there's something else going wrong here. Are you using the core `users/save-user` action, or a custom controller? Can you post the entire form?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-04T19:08:07.607Z
Comments.Text: Thanks @MatsMikkelRummelhoff for your comment. I use standard `users/save-user` action. I updated the question with the entire form.
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-04T19:34:37.510Z
Comments.Text: The form looks fine to me, at a glance. How are you verifying that "no data is passed"? Are you 100% sure that the code pulling the agent ID from the request's GET params, and the query for the associated agent, are correct? The easiest answer to the problem is that there is never a valid user ID being passed to the `associatedAgent` hidden input.
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-04T19:35:38.807Z
Comments.Text: Could it be a caching issue? Are you by any chance using `{% cache %}` around the form? I'm just guessing at this point; from what I can tell the actual form is OK.
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-07T08:20:07.827Z
Comments.Text: @MatsMikkelRummelhoff thanks for your help. I'm really puzzled. I am using no cache so far (still in dev) and the query for the associated agent is correct. It simply does not work. I mean, I have 2 other fields (fields[phone] and fields[companyName], as simple string fields) which are correctly saved, but that specific field associatedAgent is not saved at all. I'm 100% sure the agent integer is sent looking at the form payload, but I see no association on the backoffice... dunno what else to check...
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-07T08:29:26.250Z
Comments.Text: @MatsMikkelRummelhoff I updated the question with a screen of the payload of the form... as you can see the data is correctly sent... could it be that it is sent as a string while it expects an integer, which leads to no association at all?
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-07T08:45:59.743Z
Comments.Text: I forgot to mention that the agent id passed via form send is definitely an existing one, in all my tests...
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-07T09:00:03.513Z
Comments.Text: @MatsMikkelRummelhoff Myabe I find out the issue. I updated the question. Do you think it is a craft bug, as I do?
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-09T08:38:47.813Z
Comments.Text: @MatsMikkelRummelhoff just add an issue here https://github.com/craftcms/cms/issues/12283
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-09T08:39:40.407Z
Comments.Text: Good call – this seems really strange. I wouldn't think conditions would affect the logic for saving a user at all.
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-11T06:33:14.947Z
Comments.Text: @MatsMikkelRummelhoff I'm puzzled by this reply on the issue I created on github: https://github.com/craftcms/cms/issues/12283#issuecomment-1309657367 do you think there is some misunderstaing here? Thanks!
Comments.UserDisplayName: Masiorama
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-11T08:06:48.680Z
Comments.Text: It's news to me that conditional fields work like that (I thought it was more of a UI thing only), but I guess it makes sense. Probably the issue is that when the user is registered via the front-end form, they are not actually in the Clients user group yet – hence the `associatedAgent` field won't be available due to the condition, and Craft refuses to save the value for it?
Comments.UserDisplayName: Mats Mikkel Rummelhoff
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-11-11T08:42:13.313Z
Comments.Text: @MatsMikkelRummelhoff I took the liberty to mention you on github, since your hypothesis is interesting and I would like to know if brandonkelly confirms your supposition. Thanks for all your support!
Comments.UserDisplayName: Masiorama
Tags: <craft4>
Title: Attach a User Element to another User via user registration frontend form
ThreadText: # Attach a User Element to another User via user registration frontend form
  
  (Asked by: username_0 on 2022-10-31)
  
  I am trying to prepopulate a hidden field on frontend user registration from with another user element... to explain better:any client user could be connected to an agent user, if a parameter (agentId) is present in the query string on the registration form page.
  
  Looking at the backend part: I created the associatedAgent custom field, which has been added to user fields.
  
  I retrieve *agentId* and *associatedAgent* this way:
  
  ```
  {% set associatedAgentId = craft.app.request.getParam('agentId') %} 
  {% set associatedAgent = craft.users().group('agents').id(associatedAgentId).one() %}   
  
  ```
  
  In the form I am using an hiddenInput() with my custom field name *associatedAgent*. I tried with:
  
  ```
  {{ hiddenInput('fields[associatedAgent]', associatedAgent.id) }}
  
  ```
  
  and
  
  ```
  {{ hiddenInput('fields[associatedAgent][]', associatedAgent.id) }}
  
  ```
  
  but it seems that no data is passed...
  
  I also tried to look on the B and check how the user form handles that very custom field, selecting an agent by hand and looking at the payload on save, and this is what I see:
  
  [![enter image description here](https://i.sstatic.net/53nZs.jpg)](https://i.sstatic.net/53nZs.jpg)
  
  No idea how to fix it. What am I doing wrong? Thanks!
  
  Edit:
  
  ```
  {% set associatedAgentId = craft.app.request.getParam('agentId') %}
  {% set associatedAgent = craft.users().group('agents').id(associatedAgentId).one() %}
  
  <form method="post" action="" accept-charset="UTF-8">
    {{ csrfInput() }}
    {{ actionInput('users/save-user') }}
    {{ redirectInput(redirectUrl) }}
    {{ hiddenInput('fields[associatedAgent][]', associatedAgent ? associatedAgent.id : null) }}
  
    {% macro errorList(errors) %}
      {% if errors %}
        {{ ul(errors, { class: 'errors text-red-500' }) }}
      {% endif %}
    {% endmacro %}
  
    {% from _self import errorList %}
  
    <div class="field mt-3"></div>
  
    <div class="field mt-3">
      <label for="email">{{- 'Email'|t -}}</label>
      <div class="mt-1">
        {{ input('email', 'email', user.email ?? cart.email ?? null, { id: 'email', class: 'w-full border border-gray-300 hover:border-gray-500 px-4 py-2 leading-tight rounded' }) }}
        {% if user is defined %}
          {{ errorList(user.getErrors('email')) }}
        {% endif %}
      </div>
    </div>
  
    <div class="field mt-3">
      <label for="savePassword">{{- 'Password'|t -}}</label>
      <div class="mt-1">
        {{ input('password', 'password', null, { id: 'savePassword', class: 'w-full border border-gray-300 hover:border-gray-500 px-4 py-2 leading-tight rounded' }) }}
        {% if user is defined %}
          {{ errorList(user.getErrors('password')) }}
        {% endif %}
      </div>
    </div>
  
    <div class="mt-3 lg:flex lg:justify-between lg:items-center">
      <div>{{ tag('button', { type: 'submit', class: 'cm-button', text: 'Create account'|t }) }}</div>
    </div>
  </form>
  
  ```
  
  Here is the payload of the sent register form... the id is correctly sent, but it is not registered at all:
  
  [![register form payload](https://i.sstatic.net/cViu9.jpg)](https://i.sstatic.net/cViu9.jpg)
  
  Update 2, **Probably the issue is this one**: I forgot to mention that when I associate the *associatedAgent* field to *user fields* settings I add a condition, as you can see below:
  
  [![Associated agent field to user fields settings](https://i.sstatic.net/ab7oO.jpg)](https://i.sstatic.net/ab7oO.jpg)
  
  This is supposed to show the select field only for users which are indeed *clients*, because I do not want panel admins to be able to associate agents to agents, or agents to admins... well, if this setting is configured as above, the saving fails. If I clear the condition shown above than the association is correctly saved... I guess this is a bug in craft then?
  
  **Tags:** craft4
  
  > **Comments:**
  > **username_1**: `{{ hiddenInput('fields[associatedAgent][]', associatedAgent.id) }}` is the correct way to do it, so there's something else going wrong here. Are you using the core `users/save-user` action, or a custom controller? Can you post the entire form?  
  > **username_0**: Thanks @username_1 for your comment. I use standard `users/save-user` action. I updated the question with the entire form.  
  > **username_1**: The form looks fine to me, at a glance. How are you verifying that "no data is passed"? Are you 100% sure that the code pulling the agent ID from the request's GET params, and the query for the associated agent, are correct? The easiest answer to the problem is that there is never a valid user ID being passed to the `associatedAgent` hidden input.  
  > **username_1**: Could it be a caching issue? Are you by any chance using `{% cache %}` around the form? I'm just guessing at this point; from what I can tell the actual form is OK.  
  > **username_0**: @username_1 thanks for your help. I'm really puzzled. I am using no cache so far (still in dev) and the query for the associated agent is correct. It simply does not work. I mean, I have 2 other fields (fields[phone] and fields[companyName], as simple string fields) which are correctly saved, but that specific field associatedAgent is not saved at all. I'm 100% sure the agent integer is sent looking at the form payload, but I see no association on the backoffice... dunno what else to check...  
  > **username_0**: @username_1 I updated the question with a screen of the payload of the form... as you can see the data is correctly sent... could it be that it is sent as a string while it expects an integer, which leads to no association at all?  
  > **username_0**: I forgot to mention that the agent id passed via form send is definitely an existing one, in all my tests...  
  > **username_0**: @username_1 Myabe I find out the issue. I updated the question. Do you think it is a craft bug, as I do?  
  > **username_0**: @username_1 just add an issue here https://github.com/craftcms/cms/issues/12283  
  > **username_1**: Good call – this seems really strange. I wouldn't think conditions would affect the logic for saving a user at all.  
  > **username_0**: @username_1 I'm puzzled by this reply on the issue I created on github: https://github.com/craftcms/cms/issues/12283#issuecomment-1309657367 do you think there is some misunderstaing here? Thanks!  
  > **username_1**: It's news to me that conditional fields work like that (I thought it was more of a UI thing only), but I guess it makes sense. Probably the issue is that when the user is registered via the front-end form, they are not actually in the Clients user group yet – hence the `associatedAgent` field won't be available due to the condition, and Craft refuses to save the value for it?  
  > **username_0**: @username_1 I took the liberty to mention you on github, since your hypothesis is interesting and I would like to know if brandonkelly confirms your supposition. Thanks for all your support!  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-03-29. Score: 1 | Accepted answer
  
  The issue was indeed related to a bug (if we can call it like this) which was fixed in Craft 4.3.7.
  
  Here some references:
  
  * issue: <https://github.com/craftcms/cms/issues/12283>
  * pull request with bugfix: <https://github.com/craftcms/cms/pull/12544>
  
  I tested the bugfix in my specific situation and it worked as I needed, so this can be considered fixed.
  
  I posted this answer mainly for anyone with an older version of craft having trouble trying to achieve the very same goal.
  
  Thanks to @username_1 and The Craft Team for the help and for fixing this.
  
  
  ---
  
  