5

I have a registration form on my website. All of the fields here so far have been text, which is no problem. However, I've just been asked to create a dropdown box for users to select their salutation (mr, mrs, doctor etc).

I've created the field in the backend and linked it to Users, and I can update it through a front-end form no problem using the value="users/saveUser" method, and pulling the values using currentUser.salutation.options. However, when registering, I can't output the values like that as the user isn't signed in (because they're registering), so there is no currentUser. I can't use the documentation code:

{% for option in entry.dropdownFieldHandle.options %}
    <li>{{ option }}</li>
{% endfor %}

as there is no entry, I'm just on a template with a registration form on it. How do I output the dropdown options?

10

The not so elegant way to do this is to re-use the field somewhere else, where you do have access to (e.g. a global set). But you probably want to avoid this, right?

What you can do instead is to access the field directly using craft.fields. This function returns the FieldModel of your field, containing all of its properties. The FieldModel property you want is attributes.settings, where you can access your dropdown options like this:

{% set dropdownField = craft.fields.getFieldbyHandle('dropdownFieldHandle') %}

{% for option in dropdownField.settings.options %}
    {{ option.value }}
{% endfor %}
  • This worked perfectly, thank you. So glad there's a way to grab a field even when it's not part of the entry! – darylknight Aug 28 '14 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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