1

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 #}
3
  • Had you any joy with this? Currently after something similar myself. – mmc501 Oct 22 '20 at 6:37
  • Hi. Nope sorry. I had a private reply from Solspace that this is not possible at present, so the workaround approach above is the way to go. Sorry for not posting that here sooner. – Jonathan Schofield Oct 22 '20 at 12:05
  • Thanks - will implement something similar to your solution. – mmc501 Oct 22 '20 at 12:06

Your Answer

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

Browse other questions tagged or ask your own question.