Hi All,

I am using the field validator option to auto validate certain fields,
and return more meaningful error messages than the default ones returned
by formencode. It seems that there is a problem with changing some of
the messages though.

Given this sample file :

~~~~~~~~~~~~~ validatorTest.py ~~~~~~~~~~~~~~~~~~~~
from sqlobject import *
from formencode import validators, Invalid

__connection__ = "sqlite:/:memory:"


validName = validators.String(not_empty=True, min=5,
                     messages={'empty': 'The name may not be empty.',
                               'noneType': 'The name is None',
                               'tooShort': 'The name is too short',
                              }
                             )

class foo(SQLObject):
    name = StringCol(validator=validName)


foo.createTable()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When passing a value of None for 'name' when creating a new foo, this is
the message raised with the Invalid exception:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> import validatorTest as vt                                        
>>> vt.foo(name=None)                                                 
Traceback (most recent call last):                                    
  File "<stdin>", line 1, in <module>                                 
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1223,
in __init__
    self._create(id,
**kw)                                                         
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1268,
in _create 

self.set(**kw)                                                                 
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1086,
in set     
    value = to_python(dbValue,
self._SO_validatorState)                            
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 370,
in to_python
    raise Invalid(self.message('empty', state), value, state)
formencode.api.Invalid: Please enter a value
>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I would have expected to see the message "The name is None".
Also, when passing name as the empty string, the message is still
"Please enter a value" :

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> vt.foo(name='')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1223,
in __init__
    self._create(id, **kw)
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1268,
in _create
    self.set(**kw)
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1086,
in set
    value = to_python(dbValue, self._SO_validatorState)
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 370,
in to_python
    raise Invalid(self.message('empty', state), value, state)
formencode.api.Invalid: Please enter a value
>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Again, the message expected was: "The name may not be empty."
But, when passing name as a string shorter than 5 chars, the correct
message is used:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> vt.foo(name='123')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1223,
in __init__
    self._create(id, **kw)
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1268,
in _create
    self.set(**kw)
  File "/usr/lib/python2.5/site-packages/sqlobject/main.py", line 1086,
in set
    value = to_python(dbValue, self._SO_validatorState)
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 381,
in to_python
    value = tp(value, state)
  File "/usr/lib/python2.5/site-packages/formencode/compound.py", line
57, in _to_python
    to_python)
  File "/usr/lib/python2.5/site-packages/formencode/compound.py", line
118, in attempt_convert
    value = validate(validator, value, state)
  File "/usr/lib/python2.5/site-packages/formencode/compound.py", line
15, in to_python
    return validator.to_python(value, state)
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 378,
in to_python
    vo(value, state)
  File "/usr/lib/python2.5/site-packages/formencode/validators.py", line
1086, in validate_other
    value, state)
formencode.api.Invalid: The name is too short
>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The validator seems to work correctly as shown:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> vt.validName.to_python(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 370,
in to_python
    raise Invalid(self.message('empty', state), value, state)
formencode.api.Invalid: The name may not be empty.
>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

... and ...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> vt.validName.to_python('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/formencode/api.py", line 370,
in to_python
    raise Invalid(self.message('empty', state), value, state)
formencode.api.Invalid: The name may not be empty.
>>>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Anyone have any ideas why the old default formencode messages are used
for an empty or None value when specifying new messages for those
conditions?



TIA,
 Tom



------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to