{"id":439,"date":"2013-03-09T16:30:16","date_gmt":"2013-03-09T20:30:16","guid":{"rendered":"http:\/\/dashdrum.com\/blog\/?p=439"},"modified":"2013-03-09T16:30:16","modified_gmt":"2013-03-09T20:30:16","slug":"custom-errorlist-in-a-form","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2013\/03\/custom-errorlist-in-a-form\/","title":{"rendered":"Custom ErrorList in a Form"},"content":{"rendered":"<p>I was playing around with a custom ErrorList the other day. The idea is to be able modify the way errors display in a template. The Django documentation gives an <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/forms\/api\/#customizing-the-error-list-format\">example of this technique<\/a>.<\/p>\n<p>(One thing I noticed while overriding methods in ErrorList, when I only defined one of the existing display methods, such as <code>as_ul<\/code>, the code was ignored.  Only when I also included my own <code>__unicode__<\/code> method pointing to my new code did it work correctly.  I&#8217;m using Django 1.3 right now, so you may find things better in a future version.)<\/p>\n<p>My challenge was to use my custom ErrorList with a form in a class-based views environment. There are several ways to accomplish this.<\/p>\n<h2>Handle It in the View<\/h2>\n<p>In the view, one can override the <code>get_form_kwargs<\/code> method to add the <code>error_class<\/code> attribute to the <code>kwargs<\/code> dictionary.<\/p>\n<pre><code>def get_form_kwargs(self):\n\n    kwargs = super(MyViewName,self).get_form_kwargs()\n\n    kwargs.update({'error_class': MyCustomErrorList})\n\n    return kwargs\n<\/code><\/pre>\n<p>Since I&#8217;m a big mixin fan, I developed this little gem that performs this task:<\/p>\n<pre><code>class CustomErrorClassViewMixin(object):\n\n    ''' Will set the error_class attribute\n        on the form with the provided value \n        found in the error_class variable  '''\n\n    error_class = None\n\n    def get_form_kwargs(self):\n        # Make sure that the error_class attribute is set on the\n        # view, or raise a configuration error.\n        if self.error_class is None:\n            raise ImproperlyConfigured(\"'CustomErrorClassViewMixin' requires \"\n                \"'error_class' attribute to be set.\")\n\n        kwargs = super(CustomErrorClassViewMixin,self).get_form_kwargs()\n\n        kwargs.update({'error_class': self.error_class})\n\n        return kwargs\n<\/code><\/pre>\n<p>Include this mixin in your view class definition (<code>class MyView(CustomErrorClassViewMixin, CreateView)<\/code>) and set a value for the <code>error_class<\/code> variable, and it will handle the rest.<\/p>\n<h2>Form Level Fun<\/h2>\n<p>In some situations, it may be desired to use the custom ErrorList on every use of the form.  To do this, a little work in the form&#8217;s <code>__init__<\/code> method is required.<\/p>\n<pre><code>def __init__(self, *args, **kwargs):\n    ## Set the error_class attribute\n    kwargs['error_class'] = MyCustomErrorList\n\n    ## Call the parent method\n    super(MyForm, self).__init__(*args, **kwargs)\n<\/code><\/pre>\n<p>Here&#8217;s a mixin for this scenario (I love the mixins!):<\/p>\n<pre><code>class CustomErrorClassFormMixin(object):\n''' Allows the declaration of a custom error_class for a form\n\n    Requires the error_class attribute to be provided \n'''\n\n    error_class = None ## Default to none\n\n    def __init__(self, *args, **kwargs):\n        # Make sure that the error_class attribute is set on the\n        # form, or raise a configuration error.\n        if self.error_class is None:\n            raise ImproperlyConfigured(\"'CustomErrorClassFormMixin' requires \"\n                \"'error_class' attribute to be set.\")\n\n        ## Set the error_class attribute\n        kwargs['error_class'] = self.error_class\n\n        ## Call the parent method\n        super(CustomErrorClassFormMixin, self).__init__(*args, **kwargs)\n<\/code><\/pre>\n<p>A couple of notes for this one:<\/p>\n<ul>\n<li>The mixin must be declared before the form class in order to update the <code>error_class<\/code> in <code>kwargs<\/code> before the form&#8217;s <code>__init__()<\/code> method fires<\/li>\n<li>The <code>error_class<\/code> attribute must be defined<\/li>\n<\/ul>\n<h2>Best Looking Errors in Town<\/h2>\n<p>Now, your users can make gorgeous errors.  Let me know how this works for you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was playing around with a custom ErrorList the other day. The idea is to be able modify the way errors display in a template. The Django documentation gives an example of this technique. (One thing I noticed while overriding methods in ErrorList, when I only defined one of the existing display methods, such as &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2013\/03\/custom-errorlist-in-a-form\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Custom ErrorList in a Form&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-439","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=439"}],"version-history":[{"count":2,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/439\/revisions"}],"predecessor-version":[{"id":441,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/439\/revisions\/441"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}