{"id":1103,"date":"2010-12-12T02:45:19","date_gmt":"2010-12-12T06:45:19","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=1103"},"modified":"2010-12-12T02:45:19","modified_gmt":"2010-12-12T06:45:19","slug":"using-formencode-for-validation-with-colander-and-deform","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/framework\/using-formencode-for-validation-with-colander-and-deform\/","title":{"rendered":"Using FormEncode for validation with Colander and Deform"},"content":{"rendered":"<p>While working on a project, I ran across a number of emails that didn&#8217;t properly validate using Colander.  Digging into Colander&#8217;s code, the regexp used was rather basic.  Chris McDonough confirmed this and said he would welcome a newer regexp.  <\/p>\n<p>However, FormEncode&#8217;s email validation also allows one to optionally check the DNS to see if there is a valid A or MX record &#8211; validation that might be handy.  Additionally, we need to do credit card verification which is not currently included in Colander.  The quick solution is to use FormEncode&#8217;s validation routines through Colander.<\/p>\n<p>Our Form Schema:<\/p>\n<pre>\r\ndef email_validate(address):\r\n    try:\r\n        valid = formencode.validators.Email().to_python(address)\r\n        return True\r\n    except Exception as message:\r\n        return unicode(message)\r\n        \r\nclass EmailTestSchema(colander.Schema):\r\n    email = colander.SchemaNode(colander.String(),\r\n        widget = deform.widget.TextInputWidget(size=60),\r\n        validator = colander.Function(email_validate),\r\n    )\r\n<\/pre>\n<p>Our view:<\/p>\n<pre>\r\n    @action(renderer='email_test.mako')\r\n    def test(self):\r\n        schema = EmailTestSchema()\r\n        form = deform.Form(schema, buttons=('Add Email',))\r\n        if self.request.POST:\r\n            try:\r\n                appstruct = form.validate(self.request.POST.items())\r\n            except deform.ValidationFailure, e:\r\n                return {'form':e.render()}\r\n\r\n        return {'form':form.render()}\r\n<\/pre>\n<p>Now, our email validation uses FormEncode which contains some fairly detailed error messages which are passed through Colander.<\/p>\n<p>After more coding, I&#8217;ll post another solution that might answer some other issues I ran into with email validation.<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/framework\/using-formencode-for-validation-with-colander-and-deform\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>While working on a project, I ran across a number of emails that didn&#8217;t properly validate using Colander. Digging into Colander&#8217;s code, the regexp used was rather basic. Chris McDonough confirmed this and said he would welcome a newer regexp. However, FormEncode&#8217;s email validation also allows one to optionally check the DNS to see if [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/framework\/using-formencode-for-validation-with-colander-and-deform\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[163,149,51,299],"class_list":["post-1103","post","type-post","status-publish","format-standard","hentry","category-framework","tag-colander","tag-deform","tag-formencode","tag-python"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=1103"}],"version-history":[{"count":3,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1103\/revisions"}],"predecessor-version":[{"id":1106,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/1103\/revisions\/1106"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=1103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=1103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=1103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}