Revision: 9974
Updated Code
at December 1, 2008 22:46 by wackysalut
Updated Code
######################################################## development.ini / production.ini [app:main] ... lang = fr ##################################################################### project/lib/base.py class BaseController(WSGIController): ### [...snip...] def __before__(self): ### [...snip...] formencode.api.set_stdtranslation(domain="FormEncode", languages=[config.get('lang', 'fr')]) ################################################################## project/lib/helpers.py from formencode import htmlfill def p_error_formatter(error): return '<p class="error">%s</p>' % htmlfill.html_quote(error) def ol_error_formatter(error): return '<ol class="errors"><li>%s</li></ol>' % htmlfill.html_quote(error) def div_error_formatter(error): return '<div class="error">%s</div>' % htmlfill.html_quote(error) def errorform(form_content, errors): """Return an HTMLFILLed form, according to those errors""" formats = {'default': p_error_formatter, 'p': p_error_formatter, 'ol': ol_error_formatter, 'div': div_error_formatter} defs = errors.value if errors else {} errs = errors.unpack_errors() if errors else {} return htmlfill.render(form_content, defs, errs, add_attributes={'+class': ' error'}, error_formatters=formats) # # NOTE: this will add a "class='error'" to each <input /> tag also. ## ############################################################# project/contorller/prefs.py import formencode from formencode import validators from formencode import htmlfill class UniqueUsername(formencode.FancyValidator): def _to_python(self, value, state): chk = model.User.query.filter_by(username=value).first() if chk: raise formencode.Invalid( "Ce nom d'usager existe déjà ", value, state) return value class PasswdValidate(formencode.Schema): username = formencode.All(validators.String(min=6,max=20), validators.PlainText(), UniqueUsername()) passwd1 = validators.String(not_empty=True) passwd2 = validators.String(not_empty=True) chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')] ### CONTROLLER class PrefsController(BaseController): def passwd(self): return render("/prefs/passwd.html") def save_passwd(self): try: prm = PasswdValidate().to_python(request.params) except validators.Invalid, e: c.errors = e return render('/prefs/passwd.html') ##################################### project/templates/prefs/passwd.html (Mako template) ## ## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content ## ${h.errorform(form(), c.errors)} <%def name="form()" buffered="True"> <form id="editform" action="${h.url_for(action='save_passwd')}" method="post"> <ol> <li class="first"><label for="username">Username<em>*</em></label> ${h.text_field('username', size=15)}<br /> <form:error name="username" format="ol" /> </li> <li><label>Password:</label> <input type="password" name="passwd1" size="15" /> <form:error name="passwd1" /> </li> <li class="first"><label>Punch it again:</label> <input type="password" name="passwd2" size="15" /> <form:error name="passwd2" /> </li> </ol> <br /> <input type="submit" value="Save" /> </form> </%def> ################################################################# Add your own stylesheet /* with stuff :) */
Revision: 9973
Updated Code
at December 1, 2008 22:27 by wackysalut
Updated Code
######################################################## development.ini / production.ini [app:main] ... lang = fr ##################################################################### project/lib/base.py class BaseController(WSGIController): ### [...snip...] def __before__(self): ### [...snip...] formencode.api.set_stdtranslation(domain="FormEncode", languages=[config.get('lang', 'fr')]) ################################################################## project/lib/helpers.py from formencode import htmlfill def errorform(form_content, errors): """Return an HTMLFILLed form, according to those errors""" defs = errors.value if errors else {} errs = errors.unpack_errors() if errors else {} return htmlfill.render(form_content, defs, errs, add_attributes={'+class': ' error'}) # # NOTE: this will add a "class='error'" to each <input /> tag also. ## ############################################################# project/contorller/prefs.py import formencode from formencode import validators from formencode import htmlfill class UniqueUsername(formencode.FancyValidator): def _to_python(self, value, state): chk = model.User.query.filter_by(username=value).first() if chk: raise formencode.Invalid( 'That username already exists', value, state) return value class PasswdValidate(formencode.Schema): username = formencode.All(validators.String(min=6,max=20), validators.PlainText(), UniqueUsername()) passwd1 = validators.String(not_empty=True) passwd2 = validators.String(not_empty=True) chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')] ### CONTROLLER class PrefsController(BaseController): def passwd(self): return render("/prefs/passwd.html") def save_passwd(self): try: prm = PasswdValidate().to_python(request.params) except validators.Invalid, e: c.errors = e return render('/prefs/passwd.html') ##################################### project/templates/prefs/passwd.html (Mako template) ## ## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content ## ${h.errorform(form(), c.errors)} <%def name="form()" buffered="True"> <form id="editform" action="${h.url_for(action='save_passwd')}" method="post"> <ol> <li class="first"><label for="username">Username<em>*</em></label> ${h.text_field('username', size=15)}<br /> <form:error name="username" /> </li> <li><label>Password:</label> <input type="password" name="passwd1" size="15" /> <form:error name="passwd1" /> </li> <li class="first"><label>Punch it again:</label> <input type="password" name="passwd2" size="15" /> <form:error name="passwd2" /> </li> </ol> <br /> <input type="submit" value="Save" /> </form> </%def>
Revision: 9972
Updated Code
at December 1, 2008 22:23 by wackysalut
Updated Code
##################################################################### project/lib/base.py class BaseController(WSGIController): ### [...snip...] def __before__(self): ### [...snip...] formencode.api.set_stdtranslation(domain="FormEncode", languages=["fr"]) ################################################################## project/lib/helpers.py from formencode import htmlfill def errorform(form_content, errors): """Return an HTMLFILLed form, according to those errors""" defs = errors.value if errors else {} errs = errors.unpack_errors() if errors else {} return htmlfill.render(form_content, defs, errs, add_attributes={'+class': ' error'}) # # NOTE: this will add a "class='error'" to each <input /> tag also. ## ############################################################# project/contorller/prefs.py import formencode from formencode import validators from formencode import htmlfill class UniqueUsername(formencode.FancyValidator): def _to_python(self, value, state): chk = model.User.query.filter_by(username=value).first() if chk: raise formencode.Invalid( 'That username already exists', value, state) return value class PasswdValidate(formencode.Schema): username = formencode.All(validators.String(min=6,max=20), validators.PlainText(), UniqueUsername()) passwd1 = validators.String(not_empty=True) passwd2 = validators.String(not_empty=True) chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')] ### CONTROLLER class PrefsController(BaseController): def passwd(self): return render("/prefs/passwd.html") def save_passwd(self): try: prm = PasswdValidate().to_python(request.params) except validators.Invalid, e: c.errors = e return render('/prefs/passwd.html') ##################################### project/templates/prefs/passwd.html (Mako template) ## ## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content ## ${h.errorform(form(), c.errors)} <%def name="form()" buffered="True"> <form id="editform" action="${h.url_for(action='save_passwd')}" method="post"> <ol> <li class="first"><label for="username">Username<em>*</em></label> ${h.text_field('username', size=15)}<br /> <form:error name="username" /> </li> <li><label>Password:</label> <input type="password" name="passwd1" size="15" /> <form:error name="passwd1" /> </li> <li class="first"><label>Punch it again:</label> <input type="password" name="passwd2" size="15" /> <form:error name="passwd2" /> </li> </ol> <br /> <input type="submit" value="Save" /> </form> </%def>
Revision: 9971
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 1, 2008 22:21 by wackysalut
Initial Code
################################################################## project/lib/helpers.py from formencode import htmlfill def errorform(form_content, errors): """Return an HTMLFILLed form, according to those errors""" defs = errors.value if errors else {} errs = errors.unpack_errors() if errors else {} return htmlfill.render(form_content, defs, errs, add_attributes={'+class': ' error'}) # # NOTE: this will add a "class='error'" to each <input /> tag also. ## ############################################################# project/contorller/prefs.py import formencode from formencode import validators from formencode import htmlfill class UniqueUsername(formencode.FancyValidator): def _to_python(self, value, state): chk = model.User.query.filter_by(username=value).first() if chk: raise formencode.Invalid( 'That username already exists', value, state) return value class PasswdValidate(formencode.Schema): username = formencode.All(validators.String(min=6,max=20), validators.PlainText(), UniqueUsername()) passwd1 = validators.String(not_empty=True) passwd2 = validators.String(not_empty=True) chained_validators = [validators.FieldsMatch('passwd1', 'passwd2')] ### CONTROLLER class PrefsController(BaseController): def passwd(self): return render("/prefs/passwd.html") def save_passwd(self): try: prm = PasswdValidate().to_python(request.params) except validators.Invalid, e: c.errors = e return render('/prefs/passwd.html') ##################################### project/templates/prefs/passwd.html (Mako template) ## ## NOTE tue usage of: buffered="True" .. required for h.errorform() to get the content ## ${h.errorform(form(), c.errors)} <%def name="form()" buffered="True"> <form id="editform" action="${h.url_for(action='save_passwd')}" method="post"> <ol> <li class="first"><label for="username">Username<em>*</em></label> ${h.text_field('username', size=15)}<br /> <form:error name="username" /> </li> <li><label>Password:</label> <input type="password" name="passwd1" size="15" /> <form:error name="passwd1" /> </li> <li class="first"><label>Punch it again:</label> <input type="password" name="passwd2" size="15" /> <form:error name="passwd2" /> </li> </ol> <br /> <input type="submit" value="Save" /> </form> </%def>
Initial URL
Initial Description
These are code snippets from different files for a Pylons project that show a working (provided the environment) htmlfill example.
Initial Title
htmlfill and Pylons integration, form validation inside controller and Mako template example with i18n
Initial Tags
validation
Initial Language
Python