Thread: [SQLObject] Changing validator messages
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Tom C. <su...@ic...> - 2009-12-10 21:22:14
|
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 |
From: Oleg B. <ph...@ph...> - 2009-12-10 21:57:09
|
On Thu, Dec 10, 2009 at 11:18:33PM +0200, Tom Coetser wrote: > >>> vt.foo(name=None) > raise Invalid(self.message('empty', state), value, state) > formencode.api.Invalid: Please enter a value foo(name=None) raises raise Invalid(self.message('empty', state), value, state) formencode.api.Invalid: The name may not be empty. for me. I suspect you are using old buggy version of FormEncode. Oleg. -- Oleg Broytman http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Tom C. <su...@ic...> - 2009-12-11 06:53:23
|
On Thursday 10 December 2009 23:56:51 Oleg Broytman wrote: > On Thu, Dec 10, 2009 at 11:18:33PM +0200, Tom Coetser wrote: > > >>> vt.foo(name=None) > > > > raise Invalid(self.message('empty', state), value, state) > > formencode.api.Invalid: Please enter a value > > foo(name=None) raises > > raise Invalid(self.message('empty', state), value, state) > formencode.api.Invalid: The name may not be empty. > > for me. I suspect you are using old buggy version of FormEncode. And your suspicion was correct :-) The stable version in Debian Lenny seems to be formencode 1.0.1, but updating to 1.2 seems to have fixed the problem. Thanks very much. Cheers, Tom |
From: Oleg B. <ph...@ph...> - 2009-12-11 09:49:40
|
On Fri, Dec 11, 2009 at 08:54:16AM +0200, Tom Coetser wrote: > And your suspicion was correct :-) The stable version in Debian Lenny seems to > be formencode 1.0.1, but updating to 1.2 seems to have fixed the problem. SQLObject's setup.py list requirement "FormEncode>=1.1.1", and I think the bugfix I made between 1.1.0 and 1.1.1 is related to the problem. If you ran setup.py install it should upgrade FormEncode automatically, shouldn't it? I use Debian too, but I have to install a lot of software manually, especially Python modules. Debian "stable" is too stable (and I don't have time to upgrade to "testing" now). Oleg. -- Oleg Broytman http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |