<p>You could try using the <a href="https://twig.sensiolabs.org/doc/2.x/functions/attribute.html" rel="nofollow noreferrer"><code>attribute</code></a> function in twig, this should let you call dynamic properties.</p>

<p>Something like:</p>

<pre><code>{{ attribute(currentUser, question.fieldName) }}
</code></pre>

CC BY-SA 3.0
2017-04-12T15:16:26.440Z
Thanks for the suggestion, I tested it but get returned the following error: array_key_exists(): The first argument should be either a string or an integer
ChocolateOrange
CC BY-SA 3.0
2017-04-12T15:19:29.067Z
What values are you getting if you dump `question.fieldName`?
Alec Ritson
CC BY-SA 3.0
2017-04-12T18:54:20.877Z
Sorry, using the attribute function did work. I just needed to to adjust it to use question.fieldName.value so I set that as a variable as such:
    
    {% set questionField = category.fieldName.value %}
    {{ attribute(currentUser, questionField ) }}

Thanks!
ChocolateOrange
CC BY-SA 3.0
2017-04-12T15:08:54.387Z
2017-04-12T15:08:54.387Z
Alec Ritson
<p>How do I go about using a dynamic value to access a user field value? I have created a form builder for users and dynamically populate the user field based on the question's associated field. </p>

<pre><code>&lt;input name="fields[{{ question.fieldName }}]"&gt;
</code></pre>

<p>This works nicely but I am looking to pre-populate this input with a value if the user has previously provided and saved this info but I'm having trouble figuring out how to combine the field name with currentUser.</p>

<p>My first try was:</p>

<pre><code>{% set fieldName = question.fieldName %}
{{ currentUser.fieldName }}
</code></pre>

<p>But that returns the following error:</p>

<p>Craft\UserModel and its behaviors do not have a method or closure named "fieldName".</p>

<p>Any help would be greatly appreciated</p>

<variable><user-profile><usermodel><current-user>
Variable for User Field
# Variable for User Field

(Asked by: username_0 on 2017-04-12)

How do I go about using a dynamic value to access a user field value? I have created a form builder for users and dynamically populate the user field based on the question's associated field. 


This works nicely but I am looking to pre-populate this input with a value if the user has previously provided and saved this info but I'm having trouble figuring out how to combine the field name with currentUser.
My first try was:
{% set fieldName = question.fieldName %}
{{ currentUser.fieldName }}

But that returns the following error:
Craft\UserModel and its behaviors do not have a method or closure named "fieldName".
Any help would be greatly appreciated

**Tags:** variable, user-profile, usermodel, current-user


---

## 1 answers

### Answer by username_1 on 2017-04-12. Score: 2 | Accepted answer

You could try using the attribute function in twig, this should let you call dynamic properties.
Something like:
{{ attribute(currentUser, question.fieldName) }}

> **Comments:**
> **username_0**: Thanks for the suggestion, I tested it but get returned the following error: array_key_exists(): The first argument should be either a string or an integer  
> **username_1**: What values are you getting if you dump `question.fieldName`?  
> **username_0**: Sorry, using the attribute function did work. I just needed to to adjust it to use question.fieldName.value so I set that as a variable as such:          {% set questionField = category.fieldName.value %}     {{ attribute(currentUser, questionField ) }}  Thanks!  

---

