FormHelper¶
- class crispy_forms_gds.helper.FormHelper(form=None)¶
This class controls the form rendering behavior of the form passed to the
{% crispy %}
tag. It intended to be use exclusively with the ‘gds’ template pack and extends the django-crispy-forms FormHelper class by adding the following attributes to control how the form is rendered.- show_non_field_errors¶
display non-field errors at the top of the form. The default is
False
as the Design System mandates that all form errors are displayed in an Error Summary at the top of the page (above the page title and outside the <form>). Only set this toTrue
if you are not using an Error Summary.- Type
bool
- label_size¶
set the default size used for all field labels. The default value of None renders labels with the same font size as body text. To change the font size and weight use one of the pre-defined Design System sizes: ‘s’, ‘m’, ‘l’ or ‘xl’.
- Type
str
, optional
- legend_size¶
set the default size used for fields that use <legend> instead of <label> (checkboxes and radios). The default value of None renders labels with the same font size as body text. To change the font size and weight use one of the pre-defined Design System sizes: ‘s’, ‘m’, ‘l’ or ‘xl’.
- Type
str
, optional
These attributes are added as template variables. They can be overridden on each field, as required.
Examples
You use FormHelper the same way as the django-crispy-form version.
Let the FormHelper create a default layout for the form:
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper(self)
Create a custom Layout:
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.helper = FormHelper() self.helper.layout = Layout( ... )
All the existing FormHelper methods are available if you need something specific.