<p>No, I don't think there's a better way, you need to add that conditional to each select option.
But what you could do is to refactor a bit and move that logic to a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">Twig macro</a>.</p>

<pre><code>{% macro stateOption(id, name, selectedId) %}
    &lt;option value="{{ id }}"{{ id == selectedId ? ' selected="selected"' }}&gt;{{ name }}&lt;/option&gt;
{% endmacro %}
{% import _self as forms %}

{% set selectedId = craft.request.getParam('state') %}

{% set states = {
    'AL': 'Alabama',
    'AK': 'Alaska',
    'AZ': 'Arizona',
} %}

&lt;select id="state" name="state"&gt;
    &lt;option value=""&gt;Select&lt;/option&gt;

    {# Go through states and use the custom macro to generate the select options #}
    {% for id, name in states %}
        {{ forms.stateOption(id, name, selectedId) }}
    {% endfor %}
&lt;/select&gt;
</code></pre>

CC BY-SA 3.0
2015-03-24T15:05:29.443Z
Ah - this is exactly the shove I needed. (Obviously) I'm new to twig development, and was searching the docs for all the wrong things it seems. I figured I would have to do it 50x times, (yuck) but the macro is the way to roll.  Thank you very much!!
Damon
CC BY-SA 3.0
2015-03-24T12:40:53.373Z
2015-03-24T12:46:16.400Z
2015-03-24T12:46:16.400Z
carlcs
<p>I have a form that contains a static select input of U.S. States:</p>

<pre><code>&lt;select id="state" name="state" class="form-control"&gt;
&lt;option value=""&gt; Select &lt;/option&gt;
&lt;option value="AL"&gt;Alabama&lt;/option&gt;
&lt;option value="AK"&gt;Alaska&lt;/option&gt;
&lt;option value="AZ"&gt;Arizona&lt;/option&gt;
....
&lt;/select&gt;
</code></pre>

<p>I have the validation figured out on my form, and also have my text inputs getting repopulated if there are any validation errors. I am curious about the best method to repopulate my select input.</p>

<p>I can access the value that was entered via:</p>

<pre><code>{% set state = craft.request.getParam('state') %}
</code></pre>

<p>Which will give me "AL" or "AK" (whichever was chosen). This is fine, but because my list is static, do I now need to have logic on every option?</p>

<p>For example, something like this...</p>

<pre><code>&lt;option value="AL" {% if state == AL %}selected="selected" {%endif%}&gt;Alabama&lt;/option&gt;
</code></pre>

<p>Or, is there a better way all together?</p>

<p>FWIW, here is how I am repopulating my text inputs:</p>

<pre><code>value="{% if model is defined %}{{ model.name }}{% endif %}
</code></pre>

<p>Thank you!</p>

<form><validation>
Repopulate a static select input
# Repopulate a static select input

(Asked by: username_0 on 2015-03-24)

I have a form that contains a static select input of U.S. States:

 Select 
Alabama
Alaska
Arizona
....


I have the validation figured out on my form, and also have my text inputs getting repopulated if there are any validation errors. I am curious about the best method to repopulate my select input.
I can access the value that was entered via:
{% set state = craft.request.getParam('state') %}

Which will give me "AL" or "AK" (whichever was chosen). This is fine, but because my list is static, do I now need to have logic on every option?
For example, something like this...
Alabama

Or, is there a better way all together?
FWIW, here is how I am repopulating my text inputs:
value="{% if model is defined %}{{ model.name }}{% endif %}

Thank you!

**Tags:** form, validation


---

## 1 answers

### Answer by username_1 on 2015-03-24. Score: 3 | Accepted answer

No, I don't think there's a better way, you need to add that conditional to each select option.
But what you could do is to refactor a bit and move that logic to a Twig macro.
{% macro stateOption(id, name, selectedId) %}
    {{ name }}
{% endmacro %}
{% import _self as forms %}

{% set selectedId = craft.request.getParam('state') %}

{% set states = {
    'AL': 'Alabama',
    'AK': 'Alaska',
    'AZ': 'Arizona',
} %}


    Select

    {# Go through states and use the custom macro to generate the select options #}
    {% for id, name in states %}
        {{ forms.stateOption(id, name, selectedId) }}
    {% endfor %}

> **Comments:**
> **username_0**: Ah - this is exactly the shove I needed. (Obviously) I'm new to twig development, and was searching the docs for all the wrong things it seems. I figured I would have to do it 50x times, (yuck) but the macro is the way to roll.  Thank you very much!!  

---

