Discussion:
[FE-discuss] multiple input fields validation
Marcin Stępnicki
2008-12-30 13:27:43 UTC
Permalink
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? ;-)

Regards,
Marcin
Matthew Wilson
2008-12-30 14:06:59 UTC
Permalink
On Tue, Dec 30, 2008 at 8:27 AM, Marcin Stępnicki <***@gmail.com> wrote:

snip
Post by Marcin Stępnicki
<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.
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?

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

Matt
--
Matthew W
Marcin Stępnicki
2008-12-30 16:40:20 UTC
Permalink
Post by Matthew Wilson
Since you have two input elements with the same name, your form will
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.
Post by Matthew Wilson
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
Ian Bicking
2008-12-30 22:04:33 UTC
Permalink
Post by Marcin Stępnicki
Hello.
User has to fill in some fields, but I don't know how many of them
1. Child #1 birth date: ....... (+)
---------------------------------------------
2. Child #2 birth date: .......
After clicking (+) a simple Javascript/jQuery function adds another
<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
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 : ***@colorstudy.com : http://blog.ianbicking.org
Mike Orr
2008-12-30 22:58:06 UTC
Permalink
Post by Ian Bicking
Post by Marcin Stępnicki
Hello.
User has to fill in some fields, but I don't know how many of them
1. Child #1 birth date: ....... (+)
---------------------------------------------
2. Child #2 birth date: .......
After clicking (+) a simple Javascript/jQuery function adds another
<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
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? ;-)
['date1', 'date2', ...]}. You can do this in the schema with
prevalidators=[formencode.variabledecode.NestedVariables()].
...
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
Marcin Stępnicki
2008-12-31 10:40:10 UTC
Permalink
Post by Ian Bicking
['date1', 'date2', ...]}. You can do this in the schema with
prevalidators=[formencode.variabledecode.NestedVariables()].
...
errors = e.unpack_errors(encode_variables=True)
'some error'} (assuming the first field is fine and the second is not).
This should work as input to htmlfill.
Yes, it works for error messages, thank you very much (I've also made
a mistake with enumeration - I should start input id from 0, not 1).
The only problem left was defaults, but I've used variable_encode and
it works like a charm.

In case anyone is interested:

http://pastebin.com/m7b5f7494

Oh, and by the way: FormEncode sphinx documentation has invalid links
from built-in search engine. For example:

http://formencode.org/search.html?q=NestedVariables&check_keywords=yes&area=default

Gives link to:

http://formencode.org/modules/variabledecode?highlight=nestedvariables
instead of proper:
http://formencode.org/module-formencode.variabledecode.html

Thank you all for your help!

Loading...