Re: multiple input fields validation
"Mike Orr" <[email protected]> Tue, 30 Dec 2008 14:58:06 -0800
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
On Tue, Dec 30, 2008 at 2:04 PM, Ian Bicking <[email protected]> wrote: > 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. I've got a similar issue in that I need to input search criteria, which are triples of field-operator-term. I've got it partly working using ForEach, but haven't succeeded in linking the error message to the right row. I'll have more details next week when I get back to it. -- Mike Orr <[email protected]> ------------------------------------------------------------------------------