From 90992d94b8e00a769ee0cf5ddc83be14bca7ab3c Mon Sep 17 00:00:00 2001 From: jlukic Date: Thu, 22 Jan 2015 15:46:00 -0500 Subject: [PATCH] #1681, Form now prevents keypress causing repeated form submit --- src/definitions/behaviors/form.js | 65 ++++++++++++++++++------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/definitions/behaviors/form.js b/src/definitions/behaviors/form.js index 0075d4573..bafea3062 100644 --- a/src/definitions/behaviors/form.js +++ b/src/definitions/behaviors/form.js @@ -40,17 +40,18 @@ $.fn.form = function(fields, parameters) { $allModules .each(function() { var - $module = $(this), - $field = $(this).find(selector.field), - $group = $(this).find(selector.group), - $message = $(this).find(selector.message), - $prompt = $(this).find(selector.prompt), - $submit = $(this).find(selector.submit), - - formErrors = [], - - element = this, - instance = $module.data(moduleNamespace), + $module = $(this), + $field = $(this).find(selector.field), + $group = $(this).find(selector.group), + $message = $(this).find(selector.message), + $prompt = $(this).find(selector.prompt), + $submit = $(this).find(selector.submit), + + formErrors = [], + keyStuckDown = false, + + element = this, + instance = $module.data(moduleNamespace), module ; @@ -162,19 +163,22 @@ $.fn.form = function(fields, parameters) { ; } if(!event.ctrlKey && key == keyCode.enter && $field.is(selector.input) && $field.not(selector.checkbox).length > 0 ) { - module.debug('Enter key pressed, submitting form'); $submit - .addClass(className.down) - ; - $field - .one('keyup' + eventNamespace, module.event.field.keyup) + .addClass(className.pressed) ; + if(!keyStuckDown) { + $field + .one('keyup' + eventNamespace, module.event.field.keyup) + ; + module.submit(); + module.debug('Enter pressed on input submitting form'); + } + keyStuckDown = true; } }, keyup: function() { - module.verbose('Doing keyboard shortcut form submit'); - $submit.removeClass(className.down); - module.submit(); + keyStuckDown = false; + $submit.removeClass(className.pressed); }, blur: function() { var @@ -212,14 +216,17 @@ $.fn.form = function(fields, parameters) { return 'change'; } else { - return (document.createElement('input').oninput !== undefined) - ? 'input' - : (document.createElement('input').onpropertychange !== undefined) - ? 'propertychange' - : 'keyup' - ; + return module.get.inputEvent(); } }, + inputEvent: function() { + return (document.createElement('input').oninput !== undefined) + ? 'input' + : (document.createElement('input').onpropertychange !== undefined) + ? 'propertychange' + : 'keyup' + ; + }, field: function(identifier) { module.verbose('Finding field with identifier', identifier); if( $field.filter('#' + identifier).length > 0 ) { @@ -365,6 +372,12 @@ $.fn.form = function(fields, parameters) { allValid = true, apiRequest ; + + // input keydown event will fire submit repeatedly by browser default + if(keyStuckDown) { + return false; + } + // reset errors formErrors = []; $.each(validation, function(fieldName, field) { @@ -676,7 +689,7 @@ $.fn.form.settings = { className : { error : 'error', success : 'success', - down : 'down', + pressed : 'down', label : 'ui prompt label' },