I know that this is trivial, but here's some code:
class Float(FancyValidator):
"""Convert a value to a float.
Example::
>>> Float.to_python('10')
10.0
>>> Float.to_python('ten')
Traceback (most recent call last):
...
Invalid: Please enter a float value
>>> Float(min=0).to_python('-1')
Traceback (most recent call last):
...
Invalid: Please enter a float >= 0.0
>>> Float(max=0).to_python('1')
Traceback (most recent call last):
...
Invalid: Please enter a float <= 0.0
"""
messages = {
'float': "Please enter a float value",
'tooLow': "Please enter a float >= %(min)s",
'tooHigh': "Please enter a float <= %(max)s"
}
min = None
max = None
def __initargs__(self, args):
if self.min != None:
self.min = float(self.min)
if self.max != None:
self.max = float(self.max)
def _to_python(self, value, state):
try:
return float(value)
except (ValueError, TypeError):
raise Invalid(self.message('float', state), value, state)
def validate_python(self, value, state):
if self.min != None and value < self.min:
msg = self.message("tooLow", state, min=self.min)
raise Invalid(msg, value, state)
if self.max != None and value > self.max:
msg = self.message("tooHigh", state, max=self.max)
raise Invalid(msg, value, state)
_from_python = _to_python
Of course, now there's overlap between the Number and Float
validators. Hence, feel free to ignore this code. I just needed it
;)
Best Regards,
-jj
On Wed, Apr 30, 2008 at 10:57 PM, Shannon -jj Behrens <jjinux@???> wrote:
> I'm looking at the source, and I see that the Int validator can check
> that the int is within min and max, but the Number validator can't.
> Is there an easy way to check that the user has given me a Float in
> the range [0, 1]?
>
> Thanks,
> -jj
>
> --
> I, for one, welcome our new Facebook overlords!
> http://jjinux.blogspot.com/
-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
Don't miss this year's exciting event. There's still time to save $100.
Use priority code J8TL2D2.
http://ad.doubleclick.net/clk;198757673;13503038;p?
http://java.sun.com/javaone
_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss@???
https://lists.sourceforge.net/lists/listinfo/formencode-discuss