From 4360d632b91c3e17a776bc9308eba3a3915f7005 Mon Sep 17 00:00:00 2001 From: arno Date: Wed, 28 Jul 2010 14:31:33 +0200 Subject: [PATCH] contact and login js validation: determines required field automatically --- application/forms/Account.php | 2 +- public/js/account.js | 8 +------ public/js/contact.js | 7 +------ public/js/forms.js | 39 +++++++++++++++++++++++++---------- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/application/forms/Account.php b/application/forms/Account.php index d8103df..c537f72 100644 --- a/application/forms/Account.php +++ b/application/forms/Account.php @@ -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( diff --git a/public/js/account.js b/public/js/account.js index ae4e3ec..c19d30e 100644 --- a/public/js/account.js +++ b/public/js/account.js @@ -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"); diff --git a/public/js/contact.js b/public/js/contact.js index 1213e6e..e417341 100644 --- a/public/js/contact.js +++ b/public/js/contact.js @@ -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"); diff --git a/public/js/forms.js b/public/js/forms.js index 9c84d5b..738b056 100644 --- a/public/js/forms.js +++ b/public/js/forms.js @@ -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; } + }); -- 2.39.2