[Checkins] SVN: jquery.jsonform/trunk/src/jquery/jsonform/ Added initial version of JQuery based JSON form validation

Roger Ineichen roger at projekt01.ch
Thu May 24 06:29:24 EDT 2007


Log message for revision 75925:
  Added initial version of JQuery based JSON form validation

Changed:
  A   jquery.jsonform/trunk/src/jquery/jsonform/README.txt
  A   jquery.jsonform/trunk/src/jquery/jsonform/SETUP.cfg
  A   jquery.jsonform/trunk/src/jquery/jsonform/TODO.txt
  A   jquery.jsonform/trunk/src/jquery/jsonform/__init__.py
  A   jquery.jsonform/trunk/src/jquery/jsonform/browser.py
  A   jquery.jsonform/trunk/src/jquery/jsonform/configure.zcml
  A   jquery.jsonform/trunk/src/jquery/jsonform/jquery.jsonform-configure.zcml
  A   jquery.jsonform/trunk/src/jquery/jsonform/jsdoc.bat
  A   jquery.jsonform/trunk/src/jquery/jsonform/json.py
  A   jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.css
  A   jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.js

-=-
Added: jquery.jsonform/trunk/src/jquery/jsonform/README.txt
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/README.txt	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/README.txt	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,5 @@
+======
+README
+======
+
+

Added: jquery.jsonform/trunk/src/jquery/jsonform/SETUP.cfg
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/SETUP.cfg	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/SETUP.cfg	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,3 @@
+<data-files zopeskel/etc/package-includes>
+  jquery.jsonform-*.zcml
+</data-files>

Added: jquery.jsonform/trunk/src/jquery/jsonform/TODO.txt
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/TODO.txt	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/TODO.txt	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,9 @@
+====
+TODO
+====
+
+- Implement form submit via JSON
+
+- Implement JQuery overlabel plugin
+
+- Improve validation, allow error message handling additional to the coloring.

Added: jquery.jsonform/trunk/src/jquery/jsonform/__init__.py
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/__init__.py	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/__init__.py	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,16 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: layer.py 197 2007-04-13 05:03:32Z rineichen $
+"""

Added: jquery.jsonform/trunk/src/jquery/jsonform/browser.py
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/browser.py	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/browser.py	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,84 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: layer.py 197 2007-04-13 05:03:32Z rineichen $
+"""
+
+import zope.interface
+import zope.component
+from zope.viewlet import viewlet
+
+from z3c.form.interfaces import IValue
+from z3c.form.interfaces import IErrorViewSnippet
+from z3c.form.error import ErrorViewSnippet
+from z3c.form.i18n import MessageFactory as _
+
+from zif.jsonserver.interfaces import IJSONRPCRequest
+
+
+JSONFormValidateJavaScriptViewlet = viewlet.JavaScriptViewlet(
+    'jsonform.validate.js')
+
+JSONFormValidateCSSViewlet = viewlet.CSSViewlet(
+    'jsonform.validate.css')
+
+
+class JSONErrorViewSnippet(object):
+    """Error view snippet."""
+    zope.component.adapts(zope.schema.ValidationError, None, IJSONRPCRequest, 
+        None, None, None)
+    zope.interface.implements(IErrorViewSnippet)
+
+    def __init__(self, error, request, widget, field, form, content):
+        self.error = self.context = error
+        self.request = request
+        self.widget = widget
+        self.field = field
+        self.form = form
+        self.content = content
+
+    def update(self):
+        value = zope.component.queryMultiAdapter(
+            (self.context, self.request, self.widget,
+             self.field, self.form, self),
+            IValue, name='message')
+        if value is not None:
+            self.message = value.get()
+        else:
+            self.message = self.error.doc()
+
+    def render(self):
+        return self.message
+
+    def __repr__(self):
+        return '<%s for %s>' %(
+            self.__class__.__name__, self.error.__class__.__name__)
+
+
+class JSONValueErrorViewSnippet(JSONErrorViewSnippet):
+    """An error view for ValueError."""
+    zope.component.adapts(ValueError, None, IJSONRPCRequest, None, None, None)
+
+    message = _('The system could not process the given value.')
+
+    def update(self):
+        value = zope.component.queryMultiAdapter(
+            (self.context, self.request, self.widget,
+             self.field, self.form, self),
+            IValue, name='message')
+        if value is not None:
+            self.message = value.get()
+
+    def render(self):
+        return self.message
\ No newline at end of file

Added: jquery.jsonform/trunk/src/jquery/jsonform/configure.zcml
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/configure.zcml	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/configure.zcml	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,25 @@
+<configure
+    xmlns="http://namespaces.zope.org/zope"
+    xmlns:jsonrpc="http://namespaces.zope.org/jsonrpc"
+    i18n_domain="jquery">
+
+  <adapter
+      factory=".browser.JSONErrorViewSnippet"
+      trusted="True"
+      permission="zope.Public"
+      />
+  <adapter
+      factory=".browser.JSONValueErrorViewSnippet"
+      trusted="True"
+      permission="zope.Public"
+      />
+
+  <!-- json validator -->
+  <jsonrpc:view
+      for="zope.publisher.interfaces.browser.IBrowserPage"
+      class=".json.Validator"
+      methods="jsonValidate"
+      permission="zope.Public"
+      />
+
+</configure>

Added: jquery.jsonform/trunk/src/jquery/jsonform/jquery.jsonform-configure.zcml
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/jquery.jsonform-configure.zcml	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/jquery.jsonform-configure.zcml	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1 @@
+<include package="jquery.jsonform" />

Added: jquery.jsonform/trunk/src/jquery/jsonform/jsdoc.bat
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/jsdoc.bat	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/jsdoc.bat	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,8 @@
+ at echo off
+set args=--directory .\jsdoc
+set args=%args% --logo ..\..\jsdoc\logo.gif
+set args=%args% --page-footer "<div>Copyright &copy; 2007 by Projekt01 GmbH</div>"
+set args=%args% --project-name "P01, Javascript API Specification"
+set args=%args% --project-summary ..\..\jsdoc\summary.html
+set src=.
+perl ..\..\jsdoc\source\jsdoc.pl -r %args% %src%

Added: jquery.jsonform/trunk/src/jquery/jsonform/json.py
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/json.py	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/json.py	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id: views.py 93 2006-07-22 22:57:31Z roger.ineichen $
+"""
+__docformat__ = 'restructuredtext'
+
+import zope.component
+import zope.interface
+from zope.publisher.interfaces.browser import IBrowserPage
+from z3c.form.interfaces import IDataConverter
+from z3c.form.interfaces import IValidator
+from z3c.form.interfaces import IErrorViewSnippet
+from z3c.form import util
+
+from zif.jsonserver.interfaces import IJSONRPCPublisher
+from zif.jsonserver.interfaces import IJSONRPCRequest
+from zif.jsonserver.jsonrpc import MethodPublisher
+
+
+class Validator(MethodPublisher):
+
+    zope.component.adapts(IBrowserPage, IJSONRPCRequest)
+
+    zope.interface.implements(IJSONRPCPublisher)
+
+    def jsonValidate(self, id, value):
+        """Validate the value for the witdget with the given DOM field id."""
+        res = u'OK'
+        data = {}
+        errorView = None
+        self.context.updateWidgets()
+        widget = util.getWidgetById(self.context, id)
+        if widget is not None: 
+            content = self.context.widgets.content
+            form = self.context.widgets.form
+            try:
+                value = IDataConverter(widget).toFieldValue(value)
+                validator = zope.component.getMultiAdapter((content, self.request, 
+                    self.context, getattr(widget, 'field', None), widget), IValidator)
+                error = validator.validate(value)
+
+            except (zope.schema.ValidationError, ValueError), error:
+                errorView = zope.component.getMultiAdapter(
+                    (error, self.request, widget, widget.field,
+                     form, content), IErrorViewSnippet)
+                errorView.update()
+
+        if errorView is not None:
+            res = errorView.render()
+        return {'id':id, 'result':res}

Added: jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.css
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.css	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.css	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,15 @@
+/*----[ validator helper ]---------------------------------------------------*/
+
+input.validated {
+    border: 3px solid green;
+}
+input.invalide {
+    border: 3px solid red;
+}
+
+textarea.validated {
+    border: 3px solid green;
+}
+textarea.invalide {
+    border: 3px solid red;
+}

Added: jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.js
===================================================================
--- jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.js	                        (rev 0)
+++ jquery.jsonform/trunk/src/jquery/jsonform/jsonform.validate.js	2007-05-24 10:29:24 UTC (rev 75925)
@@ -0,0 +1,57 @@
+//----------------------------------------------------------------------------
+/** 
+ * @fileoverview JSON based form validation
+ * The method jsonValidate can be used for validate input fields via JSON.
+ * The p01/json/xmlhttp.js and p01/json/json.js are used for doing this. 
+ *
+ * @author Roger Ineichen dev at projekt01.ch
+ * @version Beta 0.1. 
+ */
+//----------------------------------------------------------------------------
+
+/** @private */
+function showValidationError(response) {
+	var ele = document.getElementById(response.id);
+	if (response.result == 'OK') {
+	    $(ele).removeClass('invalide');
+	    $(ele).addClass('validated');
+	} else {
+	    $(ele).addClass('invalide');
+	    $(ele).removeClass('validated');
+	}
+}
+
+
+//----------------------------------------------------------------------------
+// public API
+//----------------------------------------------------------------------------
+/**
+ * validate a input field with a JSON call.
+ * @param {string} id dom element id
+ * @param {string} value of the input field
+ * @return uses the built in showValidationError method.
+ */
+function jsonValidate(id, value) {
+	var url = viewURL;
+	var jsonProxy = getJSONRPCProxy(url);
+	jsonProxy.addMethod('jsonValidate', showValidationError);
+	jsonProxy.jsonValidate(id, value);
+}
+
+/**
+ * validate a input field with a JSON call.
+ * @return JQuery, uses the built in showValidationError callback.
+ */
+jQuery.fn.jsonValidate = function() {
+    return this.each(function(){
+        $(this).blur(function(){
+        	var url = viewURL;
+        	var id = $(this).attr("id");
+        	var value = $(this).val();
+        	var jsonProxy = getJSONRPCProxy(url);
+        	jsonProxy.addMethod('jsonValidate', showValidationError);
+        	jsonProxy.jsonValidate(id, value);
+        });
+
+    });
+};



More information about the Checkins mailing list