Created by
Valberto Carneiro
last modified
| # -*- coding: utf-8 -*-
from django.forms.utils import ErrorList
from django.utils.encoding import force_text
from django.utils.html import escape, format_html, format_html_join, html_safe
from django.utils.safestring import mark_safe
class SemanticErrorList(ErrorList):
def __unicode__(self):
return self.as_list()
def as_error_message(self):
if not self.data:
return ''
return format_html(
u'<div class="{}"><i class="circular close icon"></i>{}</div>',
u'ui error message',
format_html_join('', u'<p>{}</p>', ((force_text(e),) for e in self))
)
def as_list(self):
if not self.data:
return ''
return format_html(
u'<div class="{}">{}</div>',
u'ui list',
format_html_join('', u'<div class="item">{}</div>', ((force_text(e),) for e in self))
)
def as_pointing_label(self, position='top'):
"""
Options of position: top, left, right and bottom
"""
if not self.data:
return ''
return format_html(
u'<div class="{}"><i class="circle remove icon"></i>{}</div>',
u'ui top pointing red label',
format_html_join('', u'{}', ((force_text(e),) for e in self))
)
def __init__(self, initlist=None, error_class=None):
super(ErrorList, self).__init__(initlist)
if error_class is None:
self.error_class = 'error'
else:
self.error_class = '{}'.format(error_class)
|
| # -*- coding: utf-8 -*-
from django import template
from django.forms.utils import ErrorList
from core.utils import SemanticErrorList
register = template.Library()
@register.filter
def as_pointing_label(errors):
'''
Generate error list as semanti-ui red pointing label.
Usage as templatetag on template:
{% load errorlist %}
...
<label for="id_<your_field_name>">{% trans '<your_field_label>' %}</label>
{{ form.<your_field_name> }}
{{ form.<your_field_name>.errors|as_pointing_label }}
...
'''
if type(errors) is ErrorList:
errors = errors.as_ul()
if not errors:
return u''
errors = errors.replace(u'<ul class="errorlist">', '')
errors = errors.replace(u'</ul>', '')
errors = errors.replace(u'<li>', '')
errors = errors.split(u'</li>')
errors = [error for error in errors if error]
result = SemanticErrorList(errors)
return result.as_pointing_label()
|