Commits

Anonymous committed 039be18

WW-1058: css_xhtml validation verified to work; cleaned up xhtml validation javascript

git-svn-id: http://svn.opensymphony.com/svn/webwork/trunk@1925573baa09-0c28-0410-bef9-dab3c582ae83

Comments (0)

Files changed (13)

src/java/template/css_xhtml/form-close-validate.ftl

-<#if parameters.validate?exists>
-<script>
-    function validateForm_${parameters.id}() {
-        form = document.getElementById("${parameters.id}");
-        clearErrorParagraphs(form);
-		clearErrorLabels(form);
-
-        var errors = false;
-    <#list parameters.tagNames as tagName>
-        <#list tag.getValidators("${tagName}") as validator>
-        // field name: ${validator.fieldName}
-        // validator name: ${validator.validatorType}
-        if (form.elements['${validator.fieldName}']) {
-            field = form.elements['${validator.fieldName}'];
-            var error = "${validator.defaultMessage}";
-            <#if validator.validatorType = "required">
-            if (field.value == "") {
-                addError(field, error);
-                errors = true;
-            }
-            <#elseif validator.validatorType = "requiredstring">
-            if (field.value != null && (field.value == "" || field.value.match("\W+"))) {
-                addError(field, error);
-                errors = true;
-            }
-            <#elseif validator.validatorType = "email">
-            if (field.value != null && field.value.match(/\\b(^(\\S+@).+((\\.com)|(\\.net)|(\\.org)|(\\.info)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.biz)|(\\.ws)|(\\.us)|(\\.tv)|(\\.cc)|(\\..{2,2}))$)\\b/gi)) {
-                addError(field, error);
-                errors = true;
-            }
-            <#elseif validator.validatorType = "url">
-            if (field.value != null && field.value.match(/^(file|http):\\/\\/\\S+\\.(com|net|org|info|edu|mil|gov|biz|ws|us|tv|cc)$/i)) {
-                addError(field, error);
-                errors = true;
-            }
-            <#elseif validator.validatorType = "int">
-            if (field.value != null && (parseInt(field.value) < ${validator.min} || parseInt(field.value) > ${validator.max})) {
-                addError(field, error);
-                errors = true;
-            }
-            </#if>
-        }
-        </#list>
-    </#list>
-
-        return !errors;
-    }
-</script>
-</#if>

src/java/template/css_xhtml/form-close.ftl

 <#include "/${parameters.templateDir}/simple/form-close.ftl" />
-<#include "/${parameters.templateDir}/css_xhtml/form-close-validate.ftl" />
+<#include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" />

src/java/template/css_xhtml/form-validate.ftl

 <#if parameters.validate?default(false) == true>
-<script src="${base}/webwork/validationClient.js"></script>
 <script src="${base}/webwork/css_xhtml/validation.js"></script>
     <#if parameters.onsubmit?exists>
-        ${tag.addParameter('onsubmit', "${parameters.onblur}; return validateForm_${parameters.id}();")}
+        ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return validateForm_${parameters.id}();")}
     <#else>
         ${tag.addParameter('onsubmit', "return validateForm_${parameters.id}();")}
     </#if>

src/java/template/css_xhtml/head.ftl

+<link rel="stylesheet" href="<@ww.url value='/webwork/css_xhtml/styles.css' />" type="text/css"/>
+<#include "/${parameters.templateDir}/simple/head.ftl" />

src/java/template/css_xhtml/validation.js

-var webworkValidator = new ValidationClient("$!base/validation");
-webworkValidator.onErrors = function(input, errors) {
-	var form = input.form;
-	clearErrorParagraphs(form);
-	clearErrorLabels(form);
-
-    if (errors.fieldErrors) {
-        for (var fieldName in errors.fieldErrors) {
-            if (form.elements[fieldName].touched) {
-                for (var i = 0; i < errors.fieldErrors[fieldName].length; i++) {
-                    addError(form.elements[fieldName], errors.fieldErrors[fieldName][i]);
-                }
-            }
-        }
-    }
-}
-
-function validate(element) {
-    // mark the element as touch
-    element.touched = true;
-    var namespace = element.form.attributes['namespace'].nodeValue;
-    var actionName = element.form.attributes['name'].nodeValue;
-	webworkValidator.validate(element, namespace, actionName);
-}
-
-function clearErrorParagraphs(form) {
-    var divs = form.getElementsByTagName("div");
+function clearErrorMessages(form) {var divs = form.getElementsByTagName("div");
 
     // clear out any rows with an "errorFor" attribute
     var paragraphsToDelete = new Array();
 
 }
 
-
 function addError(e, errorText) {
     try {
         // clear out any rows with an "errorFor" of e.id

src/java/template/xhtml/form-close-validate.ftl

 <script>
     function validateForm_${parameters.id}() {
         form = document.getElementById("${parameters.id}");
-        clearErrorRows(form.childNodes[1]);
+        clearErrorMessages(form);
         clearErrorLabels(form);
 
         var errors = false;

src/java/template/xhtml/form-validate.ftl

 <#if parameters.validate?default(false) == true>
 <script src="${base}/webwork/xhtml/validation.js"></script>
-    <#if parameters.onsubmit?exists>
-        ${tag.addParameter('onsubmit', "return validateForm_${parameters.id}();${parameters.onblur}")}
-    <#else>
-        ${tag.addParameter('onsubmit', "return validateForm_${parameters.id}();")}
-    </#if>
+<#if parameters.onsubmit?exists>
+    ${tag.addParameter('onsubmit', "${parameters.onsubmit}; return validateForm_${parameters.id}();")}
+<#else>
+    ${tag.addParameter('onsubmit', "return validateForm_${parameters.id}();")}
+</#if>
 </#if>

src/java/template/xhtml/head.ftl

-<link rel="stylesheet" href="<@ww.url value='/webwork/xhtml/styles.css' />" type="text/css"/>
-<#include "/${parameters.templateDir}/simple/head.ftl" />
+\

src/java/template/xhtml/validation.js

-function clearErrorRows(table) {
+function clearErrorMessages(form) {
+    var table = form.childNodes[1];
+
     // clear out any rows with an "errorFor" attribute
     var rows = table.rows;
     var rowsToDelete = new Array();

webapps/showcase/src/webapp/WEB-INF/classes/xwork-validation.xml

             <result>quiz-success.jsp</result>
         </action>
 
+        <action name="quizClientCss" class="com.opensymphony.webwork.showcase.validation.QuizAction">
+            <result name="input">quiz-client-css.jsp</result>
+            <result>quiz-success.jsp</result>
+        </action>
+
         <action name="quizAjax" class="com.opensymphony.webwork.showcase.validation.QuizAction">
             <result name="input">quiz-ajax.jsp</result>
             <result>quiz-success.jsp</result>

webapps/showcase/src/webapp/WEB-INF/classes/xwork.xml

 <xwork>
     <include file="webwork-default.xml"/>
     
-    <include file="xwork-tmjee.xml" />
-
     <include file="config-browser.xml"/>
 
     <include file="xwork-continuations.xml"/>

webapps/showcase/src/webapp/validation/index.jsp

 		<h1>Validation Examples</h1>
 		
 		<ww:url id="quizBasic" namespace="/validation" action="quizBasic" method="input"/>
-		<ww:url id="quizClient" namespace="/validation" action="quizClient" method="input"/>
+        <ww:url id="quizClient" namespace="/validation" action="quizClient" method="input"/>
+        <ww:url id="quizClientCss" namespace="/validation" action="quizClientCss" method="input"/>
 		<ww:url id="quizAjax" namespace="/validation" action="quizAjax" method="input"/>
 		<ww:url id="fieldValidatorUrl" action="showFieldValidatorsExamples" namespace="/validation" />
 		<ww:url id="nonFieldValidatorUrl" action="showNonFieldValidatorsExamples" namespace="/validation" />
 		
 		<ul>
 			<li><ww:a href="%{quizBasic}">Validation (basic)</ww:a></li>
-        	<li><ww:a href="%{quizClient}">Validation (client)</ww:a></li>
+            <li><ww:a href="%{quizClient}">Validation (client)</ww:a></li>
+            <li><ww:a href="%{quizClientCss}">Validation (client using css_xhtml theme)</ww:a></li>
         	<li><ww:a href="%{quizAjax}">Validation (ajax)</ww:a></li>
 			<li><ww:a href="%{fieldValidatorUrl}">Field Validators</ww:a></li>
 			<li><ww:a href="%{nonFieldValidatorUrl}">Non Field Validator</ww:a></li>

webapps/showcase/src/webapp/validation/quiz-client-css.jsp

+<%@ taglib prefix="ww" uri="/webwork" %>
+
+<!-- START SNIPPET: clientCssValidation -->
+
+<html>
+<head>
+    <title>Validation - Basic</title>
+    <ww:head theme="css_xhtml"/>
+</head>
+
+<body>
+
+<ww:form method="post" theme="css_xhtml" validate="true">
+    <ww:textfield label="Name" name="name"/>
+    <ww:textfield label="Age" name="age"/>
+    <ww:textfield label="Favorite color" name="answer"/>
+    <ww:submit/>
+</ww:form>
+
+</body>
+</html>
+
+<!--  END SNIPPET: clientCssValidation -->
+