diff --git a/forms/Validator.php b/forms/Validator.php index 98afb7b75..62f254700 100755 --- a/forms/Validator.php +++ b/forms/Validator.php @@ -91,9 +91,10 @@ Behaviour.register({ validate : function(fromAnOnBlur) { initialiseForm(this, fromAnOnBlur); $code - + var error = hasHadFormError(); if(!error && fromAnOnBlur) clearErrorMessage(fromAnOnBlur); + if(error && !fromAnOnBlur) focusOnFirstErroredField(); return !error; }, diff --git a/javascript/Validator.js b/javascript/Validator.js index bdc887a8f..48add3fb9 100755 --- a/javascript/Validator.js +++ b/javascript/Validator.js @@ -1,7 +1,9 @@ var _CURRENT_FORM; +var _FIRST_ERRORED_FIELD = null; function initialiseForm(form, fromAnOnBlur) { _CURRENT_FORM = form; + _FIRST_ERRORED_FIELD = null; if(fromAnOnBlur) { limitValidationErrorsTo(fromAnOnBlur); @@ -17,6 +19,13 @@ function hasHadFormError() { return _HAS_HAD_FORM_ERROR || !_ERROR_CACHE; } +function focusOnFirstErroredField() { + try { + _FIRST_ERRORED_FIELD.focus(); + } catch(er) { + } +} + /** * Returns group with the correct classname */ @@ -251,6 +260,9 @@ function validationError(field,message, messageClass, cacheError) { // Keep a reference to it field.validationMessage = validationMessage; + // Keep a reference to the first errored field + if(field && !_FIRST_ERRORED_FIELD) _FIRST_ERRORED_FIELD = field; + // Set the attributes validationMessage.className = "message " + messageClass; validationMessage.innerHTML = message;