Discussion:
[FE-discuss] validating set of integers
Lukasz Michalski
2012-01-19 10:44:01 UTC
Permalink
Hi,

Does anyone have an example how to validate set of integers from input
string?

validators.OneOf[1,2,3,4,5] throws an exception when '1' is passed.
validators.Int() AFAIK does not allow to specify set of valid values.

Is there any method to combine those two?

Thanks,
Łukasz
Chris Lambacher
2012-01-19 14:43:10 UTC
Permalink
Hi Łukasz,

I'll assume that 'validators.OneOf[1,2,3,4]' was a typo and that you meant
validators.OneOf([1,2,3,4]).

You need to combine two validators using either All or Pipe (see
http://www.formencode.org/en/latest/modules/compound.html).
Post by Lukasz Michalski
from formencode import validators, Pipe
pv = Pipe(validators.Int(), validators.OneOf([1,2,3,4]))
pv.to_python('1')
1

All is the same but you need to put the validators in reverse order that
Post by Lukasz Michalski
from formencode import validators, All
av = All(validators.OneOf([1,2,3,4]), validators.Int())
av.to_python('1')
1
Post by Lukasz Michalski
from formencode import Invalid
... av.to_python('b')
... except Invalid, e:
... print e
...
Please enter an integer value


-Chris
Post by Lukasz Michalski
Hi,
Does anyone have an example how to validate set of integers from input
string?
validators.OneOf[1,2,3,4,5] throws an exception when '1' is passed.
validators.Int() AFAIK does not allow to specify set of valid values.
Is there any method to combine those two?
Thanks,
Łukasz
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
***@kateandchris.net
Łukasz Michalski
2012-01-21 19:26:18 UTC
Permalink
Hi Łukasz,
I'll assume that 'validators.OneOf[1,2,3,4]' was a typo and that you
meant validators.OneOf([1,2,3,4]).
You need to combine two validators using either All or Pipe (see
http://www.formencode.org/en/latest/modules/compound.html).
Many thanks. Somehow I forgot about All(), but you are right, Pipe is
more intuitive.

Thanks again,
Łukasz

Loading...