<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in Use FormEncode to verify one date precedes another</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="https://tplus1.disqus.com/use_formencode_to_verify_one_date_precedes_another_94/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 27 Dec 2012 10:54:13 -0000</lastBuildDate><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-748529351</link><description>&lt;p&gt;Hey, I'm so happy this helped you out!&lt;/p&gt;&lt;p&gt;I think formencode is a great idea because it is very important to examine all the inputs coming in to our programs.&lt;/p&gt;&lt;p&gt;But the formencode implementation is very hard to use!  Especially when you want to do more complex validation.  For example, you may want to verify some input corresponds to an allowed value stored in a database, or stored in a config file.  It is not elegant to do this!&lt;/p&gt;&lt;p&gt;It is also not elegant to coalesce values from several sources, for example, sometimes I want to draw an HTML form and fill in some of the fields to save the user time.  I want to grab those values from the query string first, but if that fails, then inspect the some data stored in a session database table.  This is possible, but not elegant, and difficult to explain to another programmer.&lt;/p&gt;&lt;p&gt;I'm working on a replacement to FormEncode, called Scrubber.  It doesn't have any of the cute syntax tricks because it doesn't use metaclasses.  At some point I will write up a description, but you use the class like this::&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;br&gt;s = MyScrubber(wsgi_environ, &lt;br&gt;    database_connection, &lt;br&gt;    config_file_wrapper) &lt;br&gt;&lt;br&gt;errors, values = s.make_errors_and_values()&lt;br&gt;&lt;br&gt;if errors:&lt;br&gt;    # re-draw the form, using the errors dictionary&lt;br&gt;    # to draw error messages, and using the values &lt;br&gt;    # dictionary to pre-populate fields.&lt;br&gt;&lt;br&gt;else:&lt;br&gt;    # do whatever you want.&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;You have to define MyScrubber like this::&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;br&gt;import scrubber&lt;br&gt;&lt;br&gt;class MyScrubber(scrubber.Scrubber):&lt;br&gt;&lt;br&gt;    # Every subclass must define this&lt;br&gt;    def make_errors_and_values(self):&lt;br&gt;        errors, values = dict(), dict()&lt;br&gt;        self.extract_date1(errors, values)&lt;br&gt;        self.extract_date2(errors, values)&lt;br&gt;        self.verify_date1_precedes_date2(errors, values)&lt;br&gt;        return errors, values&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;There is no magic introspection involved.  You have to write out the definitions of extract_date1 and extract_date2 and verify_date1_precedes_date2 yourself.  Each method gets a reference to the same errors and values dictionary, so if one of the extract_... methods failed, then the later method can check for the relevant error, and quit early.&lt;/p&gt;&lt;p&gt;Here is what extract_date1 might look like::&lt;br&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;br&gt;import datetime&lt;br&gt;def extract_date1(self, errors, values):&lt;br&gt;&lt;br&gt;    # If you are using something like webob or django &lt;br&gt;    # requests, this part will look different.&lt;br&gt;    parsed_QS = urlparse.parse_qs(&lt;br&gt;        self.wsgi_environ['QUERY_STRING'])&lt;br&gt;&lt;br&gt;    if 'date1' in parsed_QS:&lt;br&gt;        raw_date1 = parsed_QS['date1']&lt;br&gt;        try:&lt;br&gt;            values['date1'] = datetime.datetime.strptime(&lt;br&gt;                raw_date1, '%Y-%m-%d')&lt;br&gt;        except ValueError as ex:&lt;br&gt;            log.debug(ex, exc_info=1)&lt;br&gt;            values['date1'] = raw_date1&lt;br&gt;&lt;br&gt;    else:&lt;br&gt;        errors['date1'] = 'This is required field!'&lt;br&gt;&lt;br&gt;    return errors, values&lt;br&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;This pattern works really well for me.  I know some people might complain about the amount of redundant code, but I don't care any more.  I prefer an obvious verbose solution that works 100% of the time over a fancy solution that ignores weird corner cases.&lt;/p&gt;&lt;p&gt;Anyhow, thanks so much for writing.  I'm glad to know it helped you out.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Thu, 27 Dec 2012 10:54:13 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-748426364</link><description>&lt;p&gt;Thank you Matt,&lt;/p&gt;&lt;p&gt;this is indeed exactly what I was in search for myself!&lt;/p&gt;&lt;p&gt;Another way of doing this would include malforming the controller to raise a ValidationError in case end lies before start, but yours is a more elegant way. It is a pity, most of the framework documentation covers mostly the standard cases. At &lt;a href="http://tinyurl.com/bngms2t" rel="nofollow noopener" target="_blank" title="http://tinyurl.com/bngms2t"&gt;http://tinyurl.com/bngms2t&lt;/a&gt; the answer is given, but you will not find this by obvious searching terms for the date problem.&lt;/p&gt;&lt;p&gt;Again, RTFM is the answer, but your helpful pointer saved a couple of hours :)&lt;/p&gt;&lt;p&gt;Regards from Germany&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Udo Güngerich</dc:creator><pubDate>Thu, 27 Dec 2012 07:30:34 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-496270299</link><description>&lt;p&gt;you're welcome&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Fri, 13 Apr 2012 09:53:24 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-496269766</link><description>&lt;p&gt;Thanks! Good one!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Lancer</dc:creator><pubDate>Fri, 13 Apr 2012 09:52:39 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-332781223</link><description>&lt;p&gt;Wonderful!  Glad it helped.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Wed, 12 Oct 2011 13:04:55 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-332773128</link><description>&lt;p&gt;Thank you very much - I was just trying to work out how to do this! Thanks for posting!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Dave H</dc:creator><pubDate>Wed, 12 Oct 2011 12:54:02 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-299063594</link><description>&lt;p&gt;Thanks -- it took a long time for me to figure that out and I hope I saved somebody else some time.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Tue, 30 Aug 2011 18:11:32 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-299021901</link><description>&lt;p&gt;+1, dude... +1&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">David Noyes</dc:creator><pubDate>Tue, 30 Aug 2011 17:37:13 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-17030309</link><description>&lt;p&gt;from the code that really does something. However, it is really a&lt;br&gt;little too fancy for my tastes.I think a big list of examples would go a long way to make it FE more popular.I hope I'll find the time soon, to publish them somewhere&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">cheap condoms</dc:creator><pubDate>Mon, 21 Sep 2009 06:23:40 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-4017497</link><description>&lt;p&gt;Yeah, FE is a fantastic idea, but like many great ideas, the first&lt;br&gt;stab may not be the best possible implementation.&lt;/p&gt;&lt;p&gt;I think a big list of examples would go a long way to make it FE more popular.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Tue, 25 Nov 2008 21:33:02 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-4014361</link><description>&lt;p&gt;FormEncode is really cool and underrated, IMHO. The documentation is really basic and not organized very well, though, so you need to read the source and experiment at lot, to understand how all the combinations of different validators, compound validators and validation schemas work and which tricks there are to write your own validators. Also, the set of standard validators provided with the FE distribution is lacking in many respects (min/max for Integers anyone?) or plain buggy (DateTimeConverter).&lt;/p&gt;&lt;p&gt;I have a sizable collection of re-usable, custom validators for my TurboGears apps now, and I hope I'll find the time soon, to publish them somewhere.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chris Arndt</dc:creator><pubDate>Tue, 25 Nov 2008 18:25:11 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-4008001</link><description>&lt;p&gt;Yeah, it is really nice to keep all the type-conversion code separate&lt;br&gt;from the code that really does something.  However, it is really a&lt;br&gt;little too fancy for my tastes.  At some point, I want to study&lt;br&gt;validino more, because it is just a lot simpler to read.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Matt Wilson</dc:creator><pubDate>Tue, 25 Nov 2008 12:43:56 -0000</pubDate></item><item><title>Re: Use FormEncode to verify one date precedes another</title><link>http://blog.tplus1.com/blog/2008/11/25/use-formencode-to-verify-one-date-precedes-another/#comment-4007529</link><description>&lt;p&gt;FormEncode is some good stuff. Definitely worth diving into.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aaron Oliver</dc:creator><pubDate>Tue, 25 Nov 2008 12:19:37 -0000</pubDate></item></channel></rss>