]> dev.renevier.net Git - syj.git/commitdiff
contact and login js validation: determines required field automatically
authorarno <arno@renevier.net>
Wed, 28 Jul 2010 12:31:33 +0000 (14:31 +0200)
committerarno <arno@renevier.net>
Fri, 6 Aug 2010 22:09:40 +0000 (00:09 +0200)
application/forms/Account.php
public/js/account.js
public/js/contact.js
public/js/forms.js

index d8103df24e6f2c6c79e53138049ef3e309a08806..c537f725a6f051af99d761a53ca8c8e3b08c8208 100644 (file)
@@ -41,7 +41,7 @@ class Syj_Form_Account extends Zend_Form
         $pass_confirm = array('Password', 'account_password_confirm', array(
                 'label' => __("confirm password"),
                 'validators' => array($identicalValidator),
-                'required' => true
+                'allowEmpty' => false
         ));
 
         $pass_current = array('Password', 'account_password_current', array(
index ae4e3ecc213e5645bbddc22aac5a2b8379933864..c19d30ee9abd763a444452884884e49f337f60e7 100644 (file)
@@ -11,13 +11,7 @@ document.observe("dom:loaded", function() {
 
         $$('.error').invoke('remove');
 
-        errorElements = $$('#account_email, #account_password').findAll(
-            function(elt) {
-                return (!elt.check(function() {
-                    return !this.value.strip().empty();
-                }, SyjStrings.notEmptyField));
-            });
-
+        errorElements = this.checkEmptyElements(SyjStrings.notEmptyField);
 
         if (!errorElements.length) {
             control = $("account_password");
index 1213e6eb5e9c7ef9f04eb11db339a8a5eb3aef39..e41734110f7ca7426ef60a72459c6ef2048c1147 100644 (file)
@@ -10,12 +10,7 @@ document.observe("dom:loaded", function() {
 
         $$('.error').invoke('remove');
 
-        errorElements = $$('input:not([type="submit"]),textarea').findAll(
-            function(elt) {
-                return (!elt.check(function() {
-                    return !this.value.strip().empty();
-                }, SyjStrings.notEmptyField));
-            });
+        errorElements = this.checkEmptyElements(SyjStrings.notEmptyField);
 
         if (!errorElements.length) {
             control = $("contact_email");
index 9c84d5b0581e0a3097ee9ef48cc69068ef058c40..738b056f67c6840d8dd9ec8df9c97f1baea0ccf9 100644 (file)
@@ -1,5 +1,17 @@
 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
     and is published under the AGPL license. */
+Element.addMethods(['input', 'textarea'], {
+    check: function(control, callback, errorMessage) {
+        if (callback.call(control)) {
+            return true;
+        }
+        control.insert({
+            after: new Element("div", {className: 'error'}).update(errorMessage)
+        });
+        return false;
+    }
+});
+
 Element.addMethods('form', {
     focus: function(form) {
         var tofocus, error;
@@ -12,23 +24,28 @@ Element.addMethods('form', {
             tofocus = form.down('input:not([readonly],[disabled]),textarea:not([readonly][disabled])');
         }
         if (tofocus) {
-            if (error && (typeof tofocus.highlight == "function")) {
+            if (error && (typeof tofocus.highlight === "function")) {
                 tofocus.highlight('#F08080');
             }
             tofocus.focus();
             tofocus.select();
         }
-    }
-});
+    },
 
-Element.addMethods(['input', 'textarea'], {
-    check: function(control, callback, errorMessage) {
-        if (callback.call(control)) {
-            return true;
-        }
-        control.insert({
-            after: new Element("div", {className: 'error'}).update(errorMessage)
+    checkEmptyElements: function(form, errorMessage) {
+        var results = [];
+        form.select('.required').each(function(elt) {
+            var id = elt.getAttribute('for'), control = $(id);
+            if (!control) {
+                return;
+            }
+            if (!control.check(function() {
+                    return !this.value.strip().empty();
+                }, errorMessage)) {
+                results.push(control);
+            }
         });
-        return false;
+        return results;
     }
+
 });