Ronnie Stevens
Ronnie Stevens

Reputation: 25

wagtail formbuilder show 'required' in template

I am implementing a Formbuilder form in a wagtail website, but like to get the required value while looping over the form fields so I can show a required icon. The required seems to be in the query, but nothing I try shows me a result.

Upvotes: 1

Views: 337

Answers (2)

w411 3
w411 3

Reputation: 3039

Create a loop through form
django doc

{% for field in form.visible_fields %}
   <div>
      <div class="form-group">
         {{ field.errors }}
         {{ field.label_tag }}
         <input type="{{ field.field.widget.input_type }}" class="form-control"
                name="{{ field.name }}" id="{{ field.id_for_label }}"
                {% if field.field.required %}required="required"{% endif %}>
         {% comment %}you might want form field for textarea or select etc. just make conditions for those {% endcomment %}
      </div>
  </div>
 {% endfor %}

Upvotes: 2

LB Ben Johnston
LB Ben Johnston

Reputation: 5196

Wagtail form builder generates a normal Django form, so your best bet for custom styling is to explore how to style Django forms first.

I would recommend Django Crispy Forms as I have used it to customise how fields are presented and it worked well.

Their docs have a specific mention about how to override how required fields are rendered.

https://django-crispy-forms.readthedocs.io/en/latest/crispy_tag_forms.html#change-required-fields

Otherwise you could just use some custom CSS to put the icon where you need it for fields with the required attribute.

https://developer.mozilla.org/en-US/docs/Web/CSS/:required

Upvotes: -1

Related Questions