1. Mikhail Korobov
  2. django-widget-tweaks
  3. Issues
Issue #8 wontfix

error_class in render_field template tag

Alex Xela
created an issue

Atm 'error_class' works in filters only. The following code wuld enable this feqture in 'render_field' template tag as well {{{

!python

class FieldAttributeNode(Node): def render(self, context): bounded_field = self.field.resolve(context)

    def filterattrs(attrs):
        for k, v in attrs:
            if k == 'error_class':
                if hasattr(bounded_field, 'errors') and bounded_field.errors:
                    k = "class"
                else:
                    continue
            yield (k, v)

    for k, v in filterattrs(self.set_attrs):
        bounded_field = set_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
    for k, v in filterattrs(self.append_attrs):
        bounded_field = append_attr(bounded_field, '%s:%s' % (k,v.resolve(context)))
    return bounded_field

}}}

Comments (3)

  1. Mikhail Korobov repo owner

    This might be a useful feature, thanks for the take on this! I'll take a look at it (but not right now); pull request with tests and docs would make this a no-brainer.

  2. Mikhail Korobov repo owner

    This is now possible using WIDGET_ERROR_CLASS template variable.

    I think there is no need to support error_class syntax in render_field itself because error classes tend to be the same for many fields, and WIDGET_ERROR_CLASS is easier in this case; special-casing error_class attribute is also not ideal. That's why I'm closing this issue. Thanks for the patch!

  3. Log in to comment