Re: htmlfill, errors and examples
michelts <[email protected]>
| Newsgroups | gmane.comp.python.formencode |
|---|---|
| Message-ID | <[email protected]> |
Hi Ian, Sorry for sending the patch too late, do you remember this thread? I attached a patch to htmlfill.py and schema.py. I attached an test (or a use case) to the patch I sending, tell me if you need something else. Thanks! -- Michel Thadeu Sabchuk Curitiba - Brasil ------------------------------------------------------------------------- Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV _______________________________________________ FormEncode-discuss mailing list FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org https://lists.sourceforge.net/lists/listinfo/formencode-discuss
htmlfill.py.diff
(text/x-patch, 1.7 KB)
Index: htmlfill.py
===================================================================
--- htmlfill.py (reviso 2104)
+++ htmlfill.py (cpia de trabalho)
@@ -130,6 +130,14 @@
error = error.replace('\n', '<br>\n')
return error
+def default_example_formatter(example):
+ """
+ Formatter that escapes the example message, wraps it in a span with
+ class ``example-message``, with parentesis and adds a ``<br>``
+ """
+ return '<span class="example-message">(Ex.: %s)</span><br/>\n' % html_quote(example)
+
+
class FillingParser(HTMLParser.HTMLParser):
r"""
Fills HTML with default values, as in a form.
@@ -165,7 +173,7 @@
def __init__(self, defaults, errors=None, use_all_keys=False,
error_formatters=None, error_class='error',
add_attributes=None, listener=None,
- auto_error_formatter=None,
+ auto_error_formatter=None, examples=None,
text_as_default=False):
HTMLParser.HTMLParser.__init__(self)
self._content = []
@@ -177,6 +185,7 @@
self.in_select = None
self.skip_next = False
self.errors = errors or {}
+ self.examples = examples or {}
if isinstance(self.errors, (str, unicode)):
self.errors = {None: self.errors}
self.in_error = None
@@ -327,6 +336,10 @@
if error:
error = self.error_formatters[formatter](error)
self.write_text(error)
+ else:
+ example = self.examples.get(name, '')
+ if example:
+ self.write_text(default_example_formatter(example))
self.skip_next = True
self.used_errors[name] = 1
schema.py.diff
(text/x-patch, 504 B)
Index: schema.py
===================================================================
--- schema.py (reviso 2104)
+++ schema.py (cpia de trabalho)
@@ -287,6 +287,10 @@
result.extend(self.fields.values())
return result
+ def examples(cls):
+ return dict([(name, getattr(validator, 'example', None)) for name, validator in cls.fields.items()])
+ examples = classmethod(examples)
+
def format_compound_error(v, indent=0):
if isinstance(v, Exception):
try:
test_example.py
(text/x-python, 1.3 KB)
from formencode.schema import Schema
from formencode.htmlfill import FillingParser
from formencode import validators
from formencode.api import Invalid
class MySchema(Schema):
name = validators.String()
birth = validators.String(not_empty=True, example='2006-11-23')
htmlForm = '''\
Name: <input type="submit" name="name"> <form:error name="name"><br>
Birth: <input type="submit" name="birth"> <form:error name="birth"><br>
'''
# rendering the form a first time, when you do not passed values
# to the form, note that here we change the error wildcard of the birth
# field by his example defined on the schema
parser = FillingParser({}, errors={}, examples=MySchema.examples())
parser.feed(htmlForm)
parser.close()
print parser.text()
print '-----------------'
# now we simulate an error on the schema and render the form with
# the error, in this case the example is suppressed and the error
# is shown in place of it
try:
MySchema.to_python({'name':'Michel', 'birth':''})
except Invalid, e:
parser = FillingParser({}, errors=e.unpack_errors(), examples=MySchema.examples())
parser.feed(htmlForm)
parser.close()
print parser.text()
# note that the example message can be define in the validator class, in the validator
# instantiation and if it is not defined, the example is suppressed