1

How would I get a list of all the checkbox options associated with a specific field, without having an entry to loop over? I'm using the same form for creating new entries and editing existing ones. Been using {{ entry.fieldValue | default( ) }} for almost everything else.

{% if entry is defined and entry.id is not empty %}
    {% for option in entry.myFieldOptions.options %}
        <div class="checkbox">
            {% set checked = false %}

            {% if option.selected %}
                {% set checked = true %}
            {% endif %}

            {% if not checked %}
                <input type="hidden" name="fields[myFieldOptions][]" value="">
            {% endif %}

            <input type="checkbox" value="{{ option.value }}" name="fields[myFieldOptions][]" {% if option.selected %} checked="checked"{% endif %}>{{ option.label }}
        </div>
    {% endfor %}
{% else %}
    //hardcoded values
    //is there a way to reference available checkbox options directly?
{% endif %}
4

You can pull the field directly using craft.fields:

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

The options will be available under the field's settings.options property. Each option will have attributes for value and label:

{% for option in myFieldOptions.settings.options %}
    {{ option.label }} {{ option.value }}
{% endfor %}
| improve this answer | |

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.