Author: jmorliaguet
Date: Thu Feb  9 11:41:32 2006
New Revision: 2328

Added:
   cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/__init__.py
   (contents, props changed)
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/browser.py
   (contents, props changed)
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/configure.zcml
   (contents, props changed)
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/cpsskins_form_validation.pt
   (contents, props changed)
Modified:
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml
Log:

- added demo of some simple form validation



Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml
       (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/configure.zcml
       Thu Feb  9 11:41:32 2006
@@ -8,4 +8,6 @@
 
   <include package=".benchmarks" />
 
+  <include package=".forms" />
+
 </configure>

Added: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/__init__.py
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/__init__.py
    Thu Feb  9 11:41:32 2006
@@ -0,0 +1 @@
+#

Added: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/browser.py
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/browser.py
     Thu Feb  9 11:41:32 2006
@@ -0,0 +1,89 @@
+
+import re
+import time
+
+from zope.app.publisher.browser import BrowserView
+from zope.app.cache.ram import RAMCache
+
+from cpsskins import minjson as json
+
+cache = RAMCache()
+
+# Schema definition (resides on the server)
+# could be obtained from zope3 schema definitions
+schema = {
+    'firstname': {
+         'required': True,
+    },
+    'lastname': {
+         'required': True,
+    },
+    'email': {
+         'required': False,
+         'constraint': re.compile("^([0-9a-z_&.+-]+!)*[0-9a-z_&.+-]+"
+                                  "@(([0-9a-z]([0-9a-z-]*[0-9a-z])?\.)+"
+                                  
"[a-z]{2,3}|([0-9]{1,3}\.){3}[0-9]{1,3})$").match,
+    },
+}
+
+# Form data (resides on the client, can be modified by the server)
+# could be generated automatically from the schema definition
+initial_form_data = {
+    'fields': [
+        {'label': u'First name',
+         'name': u'firstname',
+         'value': u'',
+         'status': u'',
+        },
+        {'label': u'Last name',
+         'name': u'lastname',
+         'value': u'',
+         'status': u'',
+        },
+        {'label': u'Email address',
+         'name': u'email',
+         'value': u'',
+         'status': u'',
+        }
+    ]
+}
+
+class Views(BrowserView):
+
+    def getFormData(self):
+        data = cache.query('formdata', {}, initial_form_data)
+        return json.write(data)
+
+    def setFormData(self, data):
+        data = json.read(data)
+
+        form_data = cache.query('formdata', {}, initial_form_data)
+        fields = form_data['fields'][:]
+
+        for field in fields:
+            name = field['name']
+            value = data[name]
+            field_def = schema[name]
+
+            # validation
+            valid = True
+            field['status'] = u''
+
+            if field_def['required'] and not value:
+                field['status'] = u"this field is required"
+                valid = False
+
+            constraint = field_def.get('constraint')
+            if constraint is not None:
+                m = constraint(value)
+                if not m:
+                    field['status'] = u"Incorrect value" 
+                    valid = False
+
+            field['value'] = value
+
+        form_data = {'fields': fields}
+
+        cache.set(form_data, 'formdata', {})
+        return json.write(form_data)
+

Added: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/configure.zcml
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/configure.zcml
 Thu Feb  9 11:41:32 2006
@@ -0,0 +1,30 @@
+<configure
+    xmlns:browser="http://namespaces.zope.org/browser";>
+
+  <browser:page
+      for="*"
+      layer="cpsskins-test"
+      name="cpsskins_form_validation.html"
+      permission="zope.Public"
+      template="cpsskins_form_validation.pt"
+  />
+
+  <browser:pages
+      layer="cpsskins-test"
+      for="*"
+      class=".browser.Views"
+      permission="zope.Public">
+
+    <browser:page
+        name="getFormData"
+        attribute="getFormData"
+    />
+
+    <browser:page
+        name="setFormData"
+        attribute="setFormData"
+    />
+
+  </browser:pages>
+
+</configure>

Added: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/cpsskins_form_validation.pt
==============================================================================
--- (empty file)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/forms/cpsskins_form_validation.pt
    Thu Feb  9 11:41:32 2006
@@ -0,0 +1,146 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
+<html xml:lang="en" lang="en"
+      xmlns="http://www.w3.org/1999/xhtml";>
+<head>
+  <title>CPSSkins Unit test file</title>
+  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+  <script src="/++skin++cpsskins/@@/++resource++prototype.js"
+          type="text/javascript"></script>
+  <script src="/++skin++cpsskins/@@/++resource++json.js"
+          type="text/javascript"></script>
+  <script src="/++skin++cpsskins/@@/++resource++cpsskins.js"
+          type="text/javascript"></script>
+  <link rel="stylesheet" type="text/css"
+        href="/++skin++cpsskins/@@/++resource++cpsskins.css" />
+
+  <style type="text/css">
+  form {
+    border: 1px solid #ccc;
+    background-color: #f3f3f0;
+    padding: 1em;
+    font: 1.3em arial, sans-serif;
+    width: 500px;
+  }
+
+  label {
+    padding: 1em;
+  }
+
+  .status {
+    color: red;
+    padding: 1em;
+  }
+
+  button {
+    margin: 1em;
+  }
+  </style>
+
+  <script type="text/javascript">
+
+    Object.extend(Widgets, {
+
+      sampleform: function(def) {
+        var widget = Canvas.createNode({
+          tag: "form",
+          attributes: {"method": "post"}
+        });
+        return new SampleForm(widget, def);
+      }
+
+    });
+
+    SampleForm = Class.create();
+    SampleForm.prototype = Object.extend(new CPSSkins.View(), {
+
+      render: function(data) {
+        var widget = this.widget;
+        widget.innerHTML = '';
+
+        var fields = data.fields;
+
+        var renderField = this._renderField;
+        $A(fields).each(function(f) {
+          widget.appendChild(renderField(f));
+        });
+
+        var button = Canvas.createNode({
+          tag: "button",
+          attributes: {
+            "type": "submit"
+          },
+          text: "SEND!"
+        });
+        widget.appendChild(button);
+
+      },
+
+      _renderField: function(field) {
+        var p = Canvas.createNode({"tag": "p"});
+
+        var label = Canvas.createNode({
+          tag: "label",
+          text: field.label
+        });
+        var status = Canvas.createNode({
+          tag: "span",
+          classes: "status",
+          text: field.status
+        });
+        var input = Canvas.createNode({
+          tag: "input",
+          attributes: {
+            "name": field.name,
+            "type": "text",
+            "value": field.value
+          }
+        });
+        p.appendChild(label);
+        p.appendChild(input);
+        p.appendChild(status);
+        return p;
+      }
+
+    });
+
+  </script>
+
+</head>
+<body>
+
+  <h1>CPSSkins: simple form validation</h1>
+
+  <ins class="controller">
+  {"id": "form-controller",
+   "type": "form"
+  }
+  </ins>
+
+  <div id="area0">
+
+    <ins class="model">
+    {"id": "form-data",
+     "data": {
+       "fields": []
+     },
+     "storage": {
+       "type": "remote",
+       "accessors": {
+         "get": "@@getFormData",
+         "set": "@@setFormData"
+       }
+    }}
+    </ins>
+
+    <ins class="view">
+    {"widget": {
+      "type": "sampleform"
+     },
+     "model": "form-data",
+     "controller": "form-controller"
+    }
+    </ins>
+
+</body>
+</html>
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to