#from formencode.htmlform import HTMLForm import formencode from formencode import htmlfill from formencode import validators, compound, schema from WebKit.Page import Page page_style = """ Tell me about yourself

Tell me about yourself

A FormEncode example

%s """ form_template = '''
Your name:

Your age:

Your favorite color:
Red
Blue
Black
Green

''' class FormSchema(schema.Schema): name = validators.String(not_empty=True) age = validators.Int(min=13, max=99) color = compound.All(validators.Set(), validators.OneOf(['red', 'blue', 'black', 'green'])) filter_extra_fields = True allow_extra_fields = True class index(Page): def awake(self, trans): print ">>> DEBUG SimpleFlow: awake" Page.awake(self, trans) print formencode.api.get_localedir() formencode.api.set_stdtranslation( domain="FormEncode", languages=["en"], ) try: msgs = formencode.api.Validator.all_messages() print msgs for msg in msgs: print msg except: print ">>> DEBUG SimpleFlow: awake: no messages" fields = self.request().fields() try: self.rendered_form except: print ">>> DEBUG SimpleFlow: awake: rendered_form = NONE" defaults = {} errors = {} self.rendered_form = None def actions(self): print ">>> DEBUG SimpleFlow: actions" print Page.actions(self) fields = self.request().fields() for field in fields: print "%s: %s" % (field, fields[field]) return ['save'] def save(self): print ">>> DEBUG SimpleFlow: save" ok = None fields = self.request().fields() try: ok = FormSchema.to_python(fields) print ">>> DEBUG SimpleFlow: save: OK" except validators.Invalid, e: print ">>> DEBUG SimpleFlow: save: e:", type(e) errors = e.error_dict eKeys = errors.keys() for k in eKeys: print "%s: %s" % (k, type(errors[k])) print ">>> DEBUG SimpleFlow: save: e.error_dict:", errors defaults = fields # Hinweis self.rendered_form = htmlfill.render(form_template, defaults, errors) self.write(page_style % self.rendered_form) def writeContent(self): print ">>> DEBUG SimpleFlow: writeContent" if self.rendered_form is None: print ">>> DEBUG SimpleFlow: writeContent: rendered_form is NONE" defaults = {} errors = {} self.rendered_form = htmlfill.render(form_template, defaults, errors) self.write(page_style % self.rendered_form) def getDefaults(self): return dict( age='enter your age', color=['blue'])