Question

I have problems display the default errors within a form I'm creating via crispy_forms using a custom template. Namely, the text "This field is required." is not present for two fields.

Please see, here

Here's the form's init code:

        ...
        start_time = forms.TimeField(label='Start Time', required=True, input_formats=[TIME_FORMAT])
        end_time = forms.TimeField(label='End Time', required=True, input_formats=[TIME_FORMAT])
        ...
        Field('end_date', placeholder='dd/mm/yyyy'),
        Field('start_time', placeholder='hh:mm (pm/am)', template="appointments/datetimefield.html"),
        Field('end_time', placeholder='hh:mm (pm/am)', template="appointments/datetimefield.html"),

and the clean, save methods here:

Finally, the custom template:

{% load crispy_forms_field %}

<div id="div_{{ field.auto_id }}" class="form-group{% if field.errors %} has-error{% endif %}">

{% if field.label and form_show_labels %}
<label for="{{ field.id_for_label }}" class="control-label {{ label_class }}{% if field.field.required %} requiredField{% endif %}">
    {{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% endif %}

<div class="controls col-xs-8 col-md-9 col-lg-9">   
    {% crispy_field field %}
</div>
</div>
Was it helpful?

Solution

You need to show the errors in the template. Crispy forms has a template you can include for the errors: {% include 'bootstrap3/layout/help_text_and_errors.html' %} (if you're using bootstrap 3).

So your custom template might be like this:

<div id="div_{{ field.auto_id }}" class="form-group{% if field.errors %} has-error{% endif %}">

{% if field.label and form_show_labels %}
<label for="{{ field.id_for_label }}" class="control-label {{ label_class }}{% if field.field.required %} requiredField{% endif %}">
    {{ field.label|safe }}{% if field.field.required %}<span class="asteriskField">*</span>{% endif %}
</label>
{% endif %}

<div class="controls col-xs-8 col-md-9 col-lg-9">   
    {% crispy_field field %}
    {% include 'bootstrap3/layout/help_text_and_errors.html' %}
</div>
</div>

OTHER TIPS

Maybe you should check the version of crispy_forms and update it.

I also had this problem with crispy_forms == 1.3.2 .But when I updated crispy_forms into 1.4.0, the problem disappeared.

Actually, I found that just bootstrap and uni_form folders were in the crispy_forms/templates and bootstrap3 folder was not when crispy_forms == 1.3.2 installed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top