using state
Eric Abrahamsen <[email protected]> Thu, 12 Jun 2008 12:34:23 +0800
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
This is obviously a really stupid question, since I can't find the
answer by googling, but I can't figure out how to use state with a
Schema. I'm just trying to pass a list of objects into one of my
custom validators, so it can check the incoming value against existing
values, but I don't know where to insert it into the flow. No matter
where and how I insert it (and I have spent an entire morning
squinting at the source code and thinking of creative ways to do so),
it raises an error at schema.py line 135, because 'list object has no
attribute full_dict'. This is quite correct, but I don't see how it
_would_ get the 'full_dict' attribute in the first place. Here's my
entire form, if someone could put me out of my misery that would be
great.
Thanks in advance,
Eric
State should be used in the validate_python method of
KeywordValidator. This gets called in a controller method as:
formdata = IntUploadSchema().to_python(kwargs) # I assume state is
added in here somewhere?
class UploadValidator(formencode.FancyValidator):
def _to_python(self, value, state):
value = value.filename if value.filename else None
return value
class KeywordValidator(formencode.FancyValidator):
def _to_python(self,value,state):
return value.lower()
def validate_python(self,value,state):
if value in [x.keyword for x in state]:
raise formencode.Invalid("That keyword is already
taken.", value, state)
if ' ' in value:
raise formencode.Invalid("Don't put spaces in the
keyword.", value, state)
class IntUploadSchema(formencode.Schema):
upload = UploadValidator(strip=True, not_empty=True)
keyword = formencode.All(validators.UnicodeString(strip=True,
not_empty=True), KeywordValidator())
-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php