Re: multiple input fields validation

Ian Bicking <[email protected]> Tue, 30 Dec 2008 17:04:33 -0500
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Marcin Stępnicki wrote:
> Hello.
> 
> I've got the following scenario:
> 
> User has to fill in some fields, but I don't know how many of them
> will be necessary. Let's say:
> 
> 1. Child #1 birth date: .......  (+)
> ---------------------------------------------
> 2. Child #2 birth date: .......
> 
> After clicking (+) a simple Javascript/jQuery function adds another
> input field to DOM :
> 
> <input id="finish_date-1" type="text" value="" style="width: 80px;"
> name="finish_date"/>
> <input id="finish_date-2" type="text" value="" style="width: 80px;"
> name="finish_date"/>
> etc.
> 
> Unfortunately, I can't find a way to validate all fields
> simultaneously. Here's a small snippet with my failed attempts:
> 
> http://pastebin.com/f12b9ea26
> 
> Just a thought: I can simply use different names for different input
> fields and then perhaps somehow modify the validation schema depending
> on number of fields added by the user, but it seems ugly.
> 
> Is there any hope for me? ;-)

If you call variabledecode on the values, you'll get {'finish_date': 
['date1', 'date2', ...]}.  You can do this in the schema with 
prevalidators=[formencode.variabledecode.NestedVariables()].

Once you get the exception, you want to do something like:

try:
     ...
except Invalid, e:
     errors = e.unpack_errors(encode_variables=True)

Then the error dict will loko like {'finish_date-1': None, 
'finish_date-2': 'some error'} (assuming the first field is fine and the 
second is not).  This should work as input to htmlfill.



-- 
Ian Bicking : [email protected] : http://blog.ianbicking.org

------------------------------------------------------------------------------
_______________________________________________
FormEncode-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/formencode-discuss