1

I'm building a Guest Entry Form, and one of the fields is a dropdown. Everything feeds through fine, but if one of the other fields has a validation error, I can save any inputted data with the code as outlined in the Entry Form

<input id="customField" type="text" name="fields[customField]"
{%- if entry is defined %} value="{{ entry.customField }}"{% endif -%}>

But, with the dropdown, I can't get the value to stick, it always defaults to the first option (It's to grab a name title - eg, Mr. Mrs, Miss etc) on an error:

<select name="fields[honorific]">
    {% for honortitle in entry.honorific.options %}
        <option {%- if entry is defined %} value="{{ honortitle.value }}{% endif -%}">{% if entry is defined %}{{ honortitle.value }}</option>
    {% endfor %}
</select>

So, if I've already selected the option at the bottom of the list, how do I get the field to remember what I've selected if there's an error elsewhere?

Thanks, Simon

1

Digging into a past solution for checkboxes thanks to @bradbell, here's the answer!

 <select name="fields[honorific]">
     {% for honortitle in entry.honorific.options %}
         {% set chosen = false %}

         {% if honortitle.selected %}
             {% set chosen = true %}
         {% endif %}

         <option value="{{ honortitle.value }}"{% if chosen %} selected{% endif %}>{{ honortitle.label }}</option>
    {% endfor %}
 </select>

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.