After a javascript validation error from a form submission, focus on the first errored field

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.2@69634 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-01-04 23:16:54 +00:00
parent 93c98bf926
commit 67d79a907e
2 changed files with 14 additions and 1 deletions

View File

@ -94,6 +94,7 @@ Behaviour.register({
var error = hasHadFormError();
if(!error && fromAnOnBlur) clearErrorMessage(fromAnOnBlur);
if(error && !fromAnOnBlur) focusOnFirstErroredField();
return !error;
},

View File

@ -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;