mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-12 09:13:14 +00:00
Add template filters to support widgets that implement render_with_errors
This commit is contained in:
parent
b912655298
commit
31966eab4f
2 changed files with 26 additions and 2 deletions
|
|
@ -4,7 +4,7 @@
|
|||
<div class="field-content">
|
||||
<div class="input {{ input_classes }} ">
|
||||
{% block form_field %}
|
||||
{{ field }}
|
||||
{{ field|render_with_errors }}
|
||||
{% endblock %}
|
||||
|
||||
{# This span only used on rare occasions by certain types of input #}
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
<p class="help">{{ field.help_text }}</p>
|
||||
{% endif %}
|
||||
|
||||
{% if field.errors %}
|
||||
{% if field|has_unrendered_errors %}
|
||||
<p class="error-message">
|
||||
{% for error in field.errors %}
|
||||
<span>{{ error|escape }}</span>
|
||||
|
|
|
|||
|
|
@ -150,3 +150,27 @@ class EscapeScriptNode(template.Node):
|
|||
return cls(nodelist)
|
||||
|
||||
register.tag(EscapeScriptNode.TAG_NAME, EscapeScriptNode.handle)
|
||||
|
||||
|
||||
# Helpers for Widget.render_with_errors, our extension to the Django widget API that allows widgets to
|
||||
# take on the responsibility of rendering their own error messages
|
||||
@register.filter
|
||||
def render_with_errors(bound_field):
|
||||
"""
|
||||
Usage: {{ field|render_with_errors }} as opposed to {{ field }}.
|
||||
If the field (a BoundField instance) has errors on it, and the associated widget implements
|
||||
a render_with_errors method, call that; otherwise, call the regular widget rendering mechanism.
|
||||
"""
|
||||
widget = bound_field.field.widget
|
||||
if bound_field.errors and hasattr(widget, 'render_with_errors'):
|
||||
return widget.render_with_errors(bound_field.html_name, bound_field.value(), attrs={'id': bound_field.auto_id}, errors=bound_field.errors)
|
||||
else:
|
||||
return bound_field.as_widget()
|
||||
|
||||
@register.filter
|
||||
def has_unrendered_errors(bound_field):
|
||||
"""
|
||||
Return true if this field has errors that were not accounted for by render_with_errors, because
|
||||
the widget does not support the render_with_errors method
|
||||
"""
|
||||
return bound_field.errors and not hasattr(bound_field.field.widget, 'render_with_errors')
|
||||
|
|
|
|||
Loading…
Reference in a new issue