Re: multiple input fields validation

"Marcin StÄ™pnicki" <[email protected]> Tue, 30 Dec 2008 17:40:20 +0100
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
2008/12/30 Matthew Wilson <[email protected]>:
> Since you have two input elements with the same name, your form will
> submit data like:
>
> finish_date=value1&finish_date=value2
>
> So you can parse that with formencode.foreach.ForEach.   Are you
> really sure you want all the text fields to have the same name?

Thank you for response.

All text fields which are logically connected, yes :-). I'm open to
any ideas, but it just seems "clean" to me, perhaps because of my
database background :-). I'd like to treat my form as rows which are
then inserted to db, let's say I want someone to enter names and
surnames of his/her friends:

Name (UnicodeString), Surname(UnicodeString), Age(Int)

<input id="name_row-1" type="text" value="" style="width: 80px;" name="name"/>
       <input id="surname_row-1" type="text" value="" style="width: 80px;"
name="surname"/>
           <input id="age_row-1" type="text" value="" style="width:
80px;" name="age"/>

another friend:
<input id="name_row-2" type="text" value="" style="width: 80px;" name="name"/>
       <input id="surname_row-2" type="text" value="" style="width: 80px;"
name="surname"/>
           <input id="age_row-2" type="text" value="" style="width:
80px;" name="age"/>

As you've probably noticed (http://pastebin.com/f12b9ea26) I've tried
ForEach. Please look here:
http://pastebin.com/m132debac

The output is:

       <input id="name-1" type="text" value="['aaa', 'bbb']" style="width:
80px;" name="name" />
       <input id="name-2" type="text" value="['aaa', 'bbb']" style="width:
80px;" name="name" />

Note the 'value' attribute returned by htmlfill. It doesn't know where
to put corresponding values - I think it works with select/options
tags, because value is predefined and htmlfill just adds
selected="select" to the proper ones.

> Also consider using a schema-level validator that works on the whole
> dictionary of values, rather than each value separately.

Yes, that should work - I'll give each input a unique name with
prefix/postfix and then filter for validations only fields named
'name*', 'surname*' and so on. I have to admit that it just Doesn't
Feel Right, though ;-). Now, I am almost certain I've seen a Django
newforms solution to this problem which didn't involve such "dirty
tricks" ;-) - I'll try to find it.

Regards

------------------------------------------------------------------------------