Отображение ошибок валидации полей Django ModelForm в хрустящих формах

Я использую следующую ModelForm:

class ListingQuoteForm(forms.ModelForm):
    author_phone = forms.CharField(validators=[validate_phone_number])
    content = forms.CharField(
        label='Your message',
        min_length=50,
        widget=forms.Textarea(attrs={'rows': 4}),
        help_text='Please provide a few details about your race and your requirements',
    )

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super().__init__(*args, **kwargs)
        if user.is_authenticated:
            self.fields['author_name'].initial = user.full_name(strict=True)
            self.fields['author_email'].initial = user.email
            self.fields['author_email'].disabled = True

    class Meta:
        model = QuoteRequest
        fields = ('author_name', 'author_email', 'author_phone', 'content')
        labels = {
            'author_name': 'Your name',
            'author_email': 'Your email',
            'author_phone': 'Your phone number',
        }

и визуализируем его в следующем шаблоне:

{% load crispy_forms_tags %}

<div class="listing-section" id="quote-form">
    <div class="listing-section-header">
        {% if primary_category.quote_request_type == 'quote' %}
        <h2>Request a quote</h2>
        {% elif primary_category.quote_request_type == 'info' %}
        <h2>Request more info</h2>
        {% endif %}
    </div>
    <div class="listing-section-body">
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            {{ quote_form.author_name|as_crispy_field }}
            {{ quote_form.author_email|as_crispy_field }}
            {{ quote_form.author_phone|as_crispy_field }}
            {{ quote_form.content|as_crispy_field }}
            <div class="form-group">
                <button class="standard-button standard-button--submit" type="submit">Send</button>
            </div>
        </form>
    </div>
</div>

Когда проверка минимальной длины поля content не проходит, выводится сообщение следующего содержания:

enter image description here

Но когда моя пользовательская проверка номера телефона не проходит, ошибка отображается под полем следующим образом:

enter image description here

Как я могу заставить ошибку проверки номера телефона отображаться так же, как встроенная проверка минимальной длины для поля CharField?

Вы можете использовать novalidate на форме, чтобы отключить стандартное поведение html5 по показу ошибок, чтобы ошибки Django могли быть показаны.

Со:

<form method="POST" enctype="multipart/form-data" novalidate>
    {% csrf_token %}
    {{ quote_form.author_name|as_crispy_field }}
    {{ quote_form.author_email|as_crispy_field }}
    {{ quote_form.author_phone|as_crispy_field }}
    {{ quote_form.content|as_crispy_field }}
    <div class="form-group">
        <button class="standard-button standard-button--submit" type="submit">Send</button>
    </div>
</form>
Вернуться на верх