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

CC BY-SA 4.0
2023-03-29T09:03:43.717Z
2023-03-29T09:03:43.717Z
Masiorama
<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>

CC BY-SA 4.0
2022-11-03T20:33:02.403Z
`{{ 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?
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-04T19:08:07.607Z
Thanks @MatsMikkelRummelhoff for your comment. I use standard `users/save-user` action. I updated the question with the entire form.
Masiorama
CC BY-SA 4.0
2022-11-04T19:34:37.510Z
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.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-04T19:35:38.807Z
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.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-07T08:20:07.827Z
@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...
Masiorama
CC BY-SA 4.0
2022-11-07T08:29:26.250Z
@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?
Masiorama
CC BY-SA 4.0
2022-11-07T08:45:59.743Z
I forgot to mention that the agent id passed via form send is definitely an existing one, in all my tests...
Masiorama
CC BY-SA 4.0
2022-11-07T09:00:03.513Z
@MatsMikkelRummelhoff Myabe I find out the issue. I updated the question. Do you think it is a craft bug, as I do?
Masiorama
CC BY-SA 4.0
2022-11-09T08:38:47.813Z
@MatsMikkelRummelhoff just add an issue here https://github.com/craftcms/cms/issues/12283
Masiorama
CC BY-SA 4.0
2022-11-09T08:39:40.407Z
Good call – this seems really strange. I wouldn't think conditions would affect the logic for saving a user at all.
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-11T06:33:14.947Z
@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!
Masiorama
CC BY-SA 4.0
2022-11-11T08:06:48.680Z
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?
Mats Mikkel Rummelhoff
CC BY-SA 4.0
2022-11-11T08:42:13.313Z
@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!
Masiorama
<craft4>
Attach a User Element to another User via user registration frontend form
# 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:

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() %}


  {{ 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 %}

  

  
    {{- 'Email'|t -}}
    
      {{ 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 %}
    
  

  
    {{- 'Password'|t -}}
    
      {{ 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 %}
    
  

  
    {{ tag('button', { type: 'submit', class: 'cm-button', text: 'Create account'|t }) }}
  


Here is the payload of the sent register form... the id is correctly sent, but it is not registered at all:

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:

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 @MatsMikkelRummelhoff 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**: @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...  
> **username_0**: @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?  
> **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**: @MatsMikkelRummelhoff Myabe I find out the issue. I updated the question. Do you think it is a craft bug, as I do?  
> **username_0**: @MatsMikkelRummelhoff 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**: @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!  
> **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**: @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!  

---

## 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 @MatsMikkelRummelhoff and The Craft Team for the help and for fixing this.


---

