Permalink
Browse files

Revert "Merge pull request #37 from joelpurra/validate-on-submit"

This reverts commit b81e6ff, reversing
changes made to ecd8559.
  • Loading branch information...
1 parent b81e6ff commit d3c8802b135e2c81c1227d3dd32340a46eb71e11 Eric Hamilton committed Sep 19, 2012
Showing with 9 additions and 327 deletions.
  1. +0 −5 index.html
  2. +9 −56 jquery.h5validate.js
  3. +0 −266 test/test.h5validate.js
View
@@ -148,11 +148,6 @@
<p><em>Note: The click event isn't just for the mouse. It will trigger for keyboard and touch screen interactions, too.</em></p>
<code class="block">click: true</code>
</dd>
- <dt>submit</dt><dd><p>(Event) When enabled, prevents submission of a form that has invalid fields. Performs validation on all fields. If any fields are invalid, it moves focus to the first one. Defaults:</p>
- <code class="block">submit: true,<br />
- validateOnSubmit: true,<br />
- focusFirstInvalidElementOnSubmit: true</code>
- </dd>
<section>
<h2>Event API</h2>
View
@@ -80,19 +80,17 @@
patternVar: 'h5-pattern',
stripMarkup: true,
- // Run submit related checks and prevent form submission if any fields are invalid?
+ // Validate on submit?
+ // **TODO: This isn't implemented, yet.
submit: true,
- // Move focus to the first invalid field on submit?
- focusFirstInvalidElementOnSubmit: true,
-
- // When submitting, validate elements that haven't been validated yet?
- validateOnSubmit: true,
-
// Callback stubs
invalidCallback: function () {},
validCallback: function () {},
+ // When submitting, validate elements that haven't been validated yet?
+ validateOnSubmit: true,
+
// Elements to validate with allValid (only validating visible elements)
allValidSelectors: ':input:visible:not(:button):not(:disabled):not(.novalidate)',
@@ -335,8 +333,7 @@
* @returns {object} jQuery object for chaining.
*/
bindDelegation: function (settings) {
- var $this = $(this),
- $forms;
+ var $this = $(this);
// Attach patterns from the library to elements.
// **TODO: pattern / validation method matching should
// take place inside the validate action.
@@ -346,13 +343,9 @@
$('.' + settings.classPrefix + key).attr('pattern', pattern);
});
- $forms = $this.filter('form')
- .add($this.find('form'))
- .add($this.parents('form'));
-
- $forms
- .attr('novalidate', 'novalidate')
- .submit(checkValidityOnSubmitHandler);
+ $this.filter('form').attr('novalidate', 'novalidate');
+ $this.find('form').attr('novalidate', 'novalidate');
+ $this.parents('form').attr('novalidate', 'novalidate');
return this.each(function () {
var kbEvents = {
@@ -377,46 +370,6 @@
}
},
- /**
- * Event handler for the form submit event.
- * When settings.submit is enabled:
- * - prevents submission if any invalid fields are found.
- * - Optionally validates all fields.
- * - Optionally moves focus to the first invalid field.
- *
- * @param {object} evt The jQuery Event object as from the submit event.
- *
- * @returns {object} undefined if no validation was done, true if validation passed, false if validation didn't.
- */
- checkValidityOnSubmitHandler = function(evt) {
-
- var $this,
- settings = getInstance.call(this),
- allValid;
-
- if(settings.submit !== true) {
- return;
- }
-
- $this = $(this);
- allValid = $this.h5Validate('allValid', { revalidate: settings.validateOnSubmit === true });
-
- if(allValid !== true) {
- evt.preventDefault();
-
- if(settings.focusFirstInvalidElementOnSubmit === true){
- var $invalid = $(settings.allValidSelectors, $this)
- .filter(function(index){
- return $(this).h5Validate('isValid', { revalidate: false }) !== true;
- });
-
- $invalid.first().focus();
- }
- }
-
- return allValid;
- },
-
instances = [],
buildSettings = function buildSettings(options) {
@@ -102,272 +102,6 @@
$form.h5Validate('allValid');
});
- module('Submit handler');
-
- test('Disabled', 1, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input1 = $('#checked-at-submit-1');
-
- $form.h5Validate({
- submit: false
- });
-
- $input1.bind('validated', function (event, data) {
- fail('Validation should not run.');
- });
-
- $form.submit(function(evt){
-
- ok(evt.isDefaultPrevented() !== true, 'Default should not be prevented, despite the invalid fields.');
-
- // Make sure not to submit in the test code
- evt.preventDefault();
- return false;
- });
-
- $form.submit();
-
- $form.remove();
- });
-
- test('Enabled, all fields valid', 2, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input1 = $('#checked-at-submit-1');
-
- $form.h5Validate();
-
- $input1.bind('validated', function (event, data) {
- ok(data, 'Validation triggered on submit. #' + data.element.id);
- });
-
- $form.submit(function(evt) {
-
- ok(!evt.isDefaultPrevented(), 'Default should not be prevented, all fields are valid.');
-
- // Make sure not to submit in the test code
- evt.preventDefault();
- return false;
- });
-
- $form.submit();
-
- $form.remove();
- });
-
- test('Enabled, some fields invalid', 2, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1;
-
- $('<input required id="checked-at-submit-1" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input1 = $('#checked-at-submit-1');
-
- $form.h5Validate();
-
- $input1.bind('validated', function (event, data) {
- ok(data, 'Validation triggered on submit. #' + data.element.id);
- });
-
- $form.submit(function(evt) {
-
- ok(evt.isDefaultPrevented(), 'Default should be prevented, at least one field is invalid.');
-
- // Make sure not to submit in the test code
- evt.preventDefault();
- return false;
- });
-
- $form.submit();
-
- $form.remove();
- });
-
- test('Submit time validation disabled', 1, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input1 = $('#checked-at-submit-1');
-
- $form.h5Validate({
- validateOnSubmit: false
- });
-
- $input1.bind('validated', function (event, data) {
- ok(false, 'Validation should not run. #' + data.element.id);
- });
-
- $form.submit(function(evt) {
-
- equal(evt.isDefaultPrevented(), true, 'Form submission should be prevented since validation has not been performed.');
-
- // Make sure not to submit in the test code
- evt.preventDefault();
- return false;
- });
-
- $form.submit();
-
- $form.remove();
- });
-
- test('Submit time validation disabled', 2, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1,
- validationShouldRun = false;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input1 = $('#checked-at-submit-1');
-
- $form.h5Validate({
- validateOnSubmit: false
- });
-
- $input1.bind('validated', function (event, data) {
- if(validationShouldRun) {
- ok(true, 'Validation should run when called manually. #' + data.element.id);
- } else {
- ok(false, 'Validation should not run on submit. #' + data.element.id);
- }
- });
-
- $form.submit(function(evt) {
-
- equal(evt.isDefaultPrevented(), false, 'Form submission should not be prevented since validation has been performed manually.');
-
- // Make sure not to submit in the test code
- evt.preventDefault();
- return false;
- });
-
- validationShouldRun = true;
- $form.h5Validate('allValid');
-
- validationShouldRun = false;
- $form.submit();
-
- $form.remove();
- });
-
- test('Focus invalid enabled', 2, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1,
- $input2,
- $input3,
- $focused;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $('<input required id="checked-at-submit-2" />')
- .appendTo($form);
-
- $('<input required id="checked-at-submit-3" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input3 = $('#checked-at-submit-3');
-
- // Using default settings
- $form.h5Validate();
-
- // Move focus to get a baseline
- $input3.focus();
-
- $form.submit();
-
- $focused = $(':focus');
- equal($focused.length, 1, 'One element should be focused.');
- equal($focused.attr('id'), 'checked-at-submit-2', 'The second field is invalid and should be focused.');
-
- $form.remove();
- });
-
- test('Focus invalid disabled', 2, function () {
- var $form = $('<form />', {
- id: 'submitTest'
- }),
- $input1,
- $input2,
- $input3,
- $focused;
-
- $('<input required id="checked-at-submit-1" value="abc123" />')
- .appendTo($form);
-
- $('<input required id="checked-at-submit-2" />')
- .appendTo($form);
-
- $('<input required id="checked-at-submit-3" />')
- .appendTo($form);
-
- $form
- .appendTo('body');
-
- $input3 = $('#checked-at-submit-3');
-
- $form.h5Validate({
- focusFirstInvalidElementOnSubmit: false
- });
-
- // Move focus to get a baseline
- $input3.focus();
-
- $form.submit();
-
- $focused = $(':focus');
- equal($focused.length, 1, 'One element should be focused.');
- equal($focused.attr('id'), 'checked-at-submit-3', 'The third field should be still focused.');
-
- $form.remove();
- });
-
- module('Issues');
-
test('Issue #29: Disabled fields gum up the works.', function () {
var $form = $('<form>', {
id: 'disabledTest'

0 comments on commit d3c8802

Please sign in to comment.