Re: Problems with Formencode Msgs, Unexpected Inputs
"Tom Longson (nym)" <[email protected]> Fri, 3 Oct 2008 11:16:24 -0700
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Don't you hate it when you forget to add the attachment? On Fri, Oct 3, 2008 at 11:15 AM, Tom Longson (nym) <[email protected]>wrote: > Hi, > > I'm trying to get formencode to work, and I have a few problems: > > 1. How do you supply custom error messages? I've read the documentation, > and couldn't find a concrete example using preexisting validators. > 2. Why am i getting this error list with the included test file? > > List of dictionaries: > [{'name': 'Tom'}, {'email': 'tom-1ejvVKqOCHmevrI51Aqiyd1ocvOgFGy0LpsAlfeDtJ10ubjbjo6WXg@public.gmane.org'}, > {'password': 'narfffff0'}, {'password_confirm': 'narfffff0'}] > > Error list: > [Invalid('email: Missing value\npassword: Missing value\npassword_confirm: > Missing value',), Invalid('name: Missing value\npassword: Missing > value\npassword_confirm: Missing value',), Invalid('email: Missing > value\nname: Missing value\npassword_confirm: Missing value',), > Invalid('email: Missing value\nname: Missing value\npassword: Missing > value',)] > > 3. If I supply a value to the list of dictionaries that is not expected by > the Schema (e.g. "foobar"), how do I get the Schema to ignore it instead of > throwing an Invalid(u"The input field '%s' was not expected.",) ? > > Thanks! > nym > ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/ _______________________________________________ FormEncode-discuss mailing list FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/formencode-discuss
formencode_scratch.py
(text/x-python, 1.6 KB)
import formencode
from formencode import validators
usernames = ['narf', 'point']
tuplelist = [('name', u'Tom'), ('email', u'[email protected]'), ('password', u'narfffff0'), ('password_confirm', u'narfffff0')]
class SecurePassword(formencode.FancyValidator):
words_filename = '/usr/share/dict/words'
def _to_python(self, value, state):
f = open(self.words_filename)
lower = value.strip().lower()
for line in f:
if line.strip().lower() == lower:
raise formencode.Invalid(
'Please do not base your password on a '
'dictionary term', value, state)
return value
class UniqueUsername(formencode.FancyValidator):
def _to_python(self, value, state):
if value in usernames:
raise formencode.Invalid(
'That username already exists',
value, state)
return value
class Registration(formencode.Schema):
name = validators.String(not_empty=True)
email = validators.Email(resolve_domain=False, badDomain="yer fucked")
#username = formencode.All(validators.PlainText(), UniqueUsername())
password = SecurePassword()
#password_confirm = validators.String()
chained_validators = [validators.FieldsMatch('password', 'password_confirm')]
validator = formencode.ForEach(Registration())
dictlist = [{k: v.encode('ascii', 'ignore')} for k, v in tuplelist]
print "List of dictionaries:"
print dictlist
print
try:
params = validator.to_python(dictlist)
except formencode.Invalid, e:
print "Error list:"
print e.error_list
print