1

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>

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, almost there but the text below the field is black and not in red, ideas? imgur.com/AZJL9SI
Oh my bad. You're using bootstrap 3 right? The include should be bootstrap3/layout/help_text_and_errors.html not bootstrap/layout/help_text_and_errors.html. I've updated my answer.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.