TransWikia.com

Freeform: query form object for presence of field type (or other criterion)

Craft CMS Asked on December 8, 2020

I want to be able to check if a form object returned by a query has fields of a certain scope before I generate its markup.

I have a test case where I want to know if a form has a checkbox field type in it, but I can’t figure out how to query just the fields in the form with something like:

{% set hasCheckbox = form.fields.type('checkbox').one() ?? null %}

I know that doesn’t work, so I’ve got a workaround, but it would be good to know if this kind of thing is possible.


My workaround is to do something like:

{% set hasCheckbox = false %}
{% set formMarkup  = [] %}

{% for row in form %}
    {% for field in row %}
        {% set fieldMarkup %}
            {# Do stuff #}
        {% endset %}

        {% if field.type == 'checkbox' %}
            {% set hasCheckbox = true %}
        {% endif %}

        {% set formMarkup = formMarkup|merge([fieldMarkup])" %}
    {% endfor %}
{% endfor %}

{# Do something with hasCheckbox #}
{# Output formMarkup #}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP