Commits

Christoph Zwerschke committed 11e8f36

Webware example was broken (HTMLForm does not exist any more and some other minor problems).

  • Participants
  • Parent commits 0a0212e

Comments (0)

Files changed (1)

examples/WebwareExamples/index.py

-from formencode.htmlform import HTMLForm
-from formencode import validators, compound, schema
+from formencode import compound, Invalid, htmlfill, Schema, validators
+
 from WebKit.Page import Page
 
 
-page_style = """
+page_style = '''
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 <html>
  <head>
+  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <title>Tell me about yourself</title>
   <style type="text/css">
     .error {background-color: #ffdddd}
 
  %s
 
- </body></html>"""
+ </body></html>'''
 
 form_template = '''
 <form action="" method="POST">
-<input type="hidden" name="_action_" value="save">
 
-Your name:<br>
+<p>Your name:<br>
 <form:error name="name">
-<input type="text" name="name">
-<br>
+<input type="text" name="name"></p>
 
-Your age:<br>
+<p>Your age:<br>
 <form:error name="age">
-<input type="text" name="age">
-<br>
+<input type="text" name="age"></p>
 
-Your favorite color:<br>
+<p>Your favorite color:<br>
 <form:error name="color">
-<input type="checkbox" value="red"> Red<br>
-<input type="checkbox" value="blue"> Blue<br>
-<input type="checkbox" value="black"> Black<br>
-<input type="checkbox" value="green"> Green<br>
-<br>
+<input type="checkbox" value="red" name="color"> Red<br>
+<input type="checkbox" value="blue" name="color"> Blue<br>
+<input type="checkbox" value="black" name="color"> Black<br>
+<input type="checkbox" value="green" name="color"> Green<br>
+<input type="checkbox" value="pink" name="color"> Pink</p>
 
-<input type="submit" value="submit">
-</form>
-'''
+<input type="submit" name="_action_save" value="Submit">
+</form>'''
 
-class FormSchema(schema.Schema):
+response_template = '''
+<h2>Hello, %(name)s!</h2>
+<p>You are %(age)d years old
+and your favorite color is %(color)s.</p>'''
+
+
+class FormSchema(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']))
+    color = validators.OneOf(['red', 'blue', 'black', 'green'])
     filter_extra_fields = True
     allow_extra_fields = True
 
 
     def awake(self, trans):
         Page.awake(self, trans)
-        self.form = HTMLForm(form_template, FormSchema)
         self.rendered_form = None
 
     def actions(self):
         return ['save']
 
     def save(self):
-        results, errors = self.form.validate(
-            self.request().fields(), self)
-        if results is not None:
-            self.doAction(results)
+        fields = self.request().fields()
+        try:
+            fields = FormSchema.to_python(fields, self)
+        except Invalid, e:
+            errors = dict((k, v.encode('utf-8'))
+                for k, v in e.unpack_errors().iteritems())
+            print "Errors:", errors
+            self.rendered_form = htmlfill.render(form_template,
+                defaults=fields, errors=errors)
+            self.writeHTML()
         else:
-            print "Errors:", errors
-            self.rendered_form = self.form.render(
-                defaults=self.request().fields(),
-                errors=errors)
-            self.writeHTML()
-            
-    def writeContent(self):
+            self.doAction(fields)
+
+    def doAction(self, fields):
+        print "Fields:", fields
+        self.rendered_form = response_template % fields
+        self.writeHTML()
+
+    def writeHTML(self):
         if self.rendered_form is None:
-            self.rendered_form = self.form.render(
+            self.rendered_form = htmlfill.render(form_template,
                 defaults=self.getDefaults())
         self.write(page_style % self.rendered_form)
-        
+
     def getDefaults(self):
-        return dict(
-            age='enter your age',
-            color=['blue'])
+        return dict(age='enter your age', color=['blue'])
 
     def preAction(self, trans):
         pass