|
From: Ian B. <ia...@co...> - 2006-01-03 06:44:48
|
Igor Murashkin wrote:
> Hello everyone,
>
> I am trying to validate a really simple form, something like this:
> { 'names' : ['James', 'Book'], 'zips':['12345', '20093'],
> 'id':'5', 'misc':'foo'] }
>
> for a method like processForm(names, zips, id, misc)
>
> However, while making a schema for id and misc is simple (using
> validators.Int() and validators.String()), I have hit a dead end when
> looking for a way to validate a *list* of integers or a list of
> strings.
>
> According to the FormEncode documentation,
> http://www.formencode.org/module-formencode.foreach.html,
> ForEach(AsInt(), InList([1, 2, 3])) would validate the [1,2,3] as a
> list of integers. However I cannot find AsInt() or InList() definitions
> *anywhere* in formencode package or in all of turbogears for that
> matter (yes, I grepped it).
I think the documentation is out of date, it should be
foreach.ForEach(validators.OneOf([1, 2, 3]), validators.Int()), which
should be equivalent to ForEach(All(validators.OneOf([1, 2, 3]),
validators.Int()))
> I know there is a way of writing custom validators, but I don't think
> that's pythonic or even correct at all. It would be like
> copying-and-pasting every time I needed to validate a list. There's got
> to be an existing way to validate lists of something, right?
ForEach is what you want; it takes as arguments validator(s) that will
be applied to a list.
--
Ian Bicking | ia...@co... | http://blog.ianbicking.org
|