|
13 | 13 |
|
14 | 14 | 'use strict';
|
15 | 15 |
|
16 |
| - var setupValidationDependsOn = function($form) { |
| 16 | + var setupValidationDependsOn = function($form, conf) { |
17 | 17 |
|
18 |
| - $form.find('[data-validation-depends-on]') |
19 |
| - .on('beforeValidation', function() { |
| 18 | + var dependingOnBeforeValidation = function() { |
20 | 19 |
|
21 |
| - var $elem = $(this), |
22 |
| - nameOfDependingInput = $elem.valAttr('depends-on') || $elem.valAttr('if-checked'); |
| 20 | + var $elem = $(this), |
| 21 | + nameOfDependingInput = $elem.valAttr('depends-on') || $elem.valAttr('if-checked'); |
23 | 22 |
|
24 |
| - // Whether or not this input should be validated depends on if another input has a value |
25 |
| - if (nameOfDependingInput) { |
| 23 | + // Whether or not this input should be validated depends on if another input has a value |
| 24 | + if (nameOfDependingInput) { |
26 | 25 |
|
27 |
| - // Set the boolean telling us that the validation depends |
28 |
| - // on another input being checked |
29 |
| - var valueOfDependingInput = $.formUtils.getValue('input[name="' + nameOfDependingInput + '"]', $form), |
30 |
| - requiredValueOfDependingInput = $elem.valAttr('depends-on-value'), |
31 |
| - dependingInputHasRequiredValue = !requiredValueOfDependingInput || requiredValueOfDependingInput === valueOfDependingInput; |
| 26 | + // Set the boolean telling us that the validation depends |
| 27 | + // on another input being checked |
| 28 | + var valueOfDependingInput = $.formUtils.getValue('[name="' + nameOfDependingInput + '"]', $form), |
| 29 | + requiredValueOfDependingInput = $elem.valAttr('depends-on-value'), |
| 30 | + dependingInputIsMissingValueOrHasIncorrectValue = !valueOfDependingInput || ( |
| 31 | + requiredValueOfDependingInput && |
| 32 | + requiredValueOfDependingInput !== valueOfDependingInput |
| 33 | + ); |
32 | 34 |
|
33 |
| - if (!dependingInputHasRequiredValue) { |
34 |
| - $elem.valAttr('skipped', true); |
35 |
| - } |
| 35 | + if (dependingInputIsMissingValueOrHasIncorrectValue) { |
| 36 | + $elem.valAttr('skipped', '1'); |
36 | 37 | }
|
37 | 38 |
|
| 39 | + } |
| 40 | + }, |
| 41 | + dependingOnValueChanged = function() { |
| 42 | + var $input = $(this), |
| 43 | + valueOfDependingInput = $.formUtils.getValue($input), |
| 44 | + requiredValueOfDependingInput = $input.valAttr('depending-value'), |
| 45 | + dependingInputIsMissingValueOrHasIncorrectValue = !valueOfDependingInput || ( |
| 46 | + requiredValueOfDependingInput && |
| 47 | + requiredValueOfDependingInput !== valueOfDependingInput |
| 48 | + ); |
| 49 | + |
| 50 | + if (dependingInputIsMissingValueOrHasIncorrectValue) { |
| 51 | + console.log(this.$dependingInput); |
| 52 | + $.formUtils.dialogs.removeInputStylingAndMessage(this.$dependingInput, conf); |
| 53 | + } else if ($.formUtils.getValue(this.$dependingInput)) { |
| 54 | + this.$dependingInput.validate(); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + $form.find('[data-validation-depends-on]') |
| 59 | + .off('beforeValidation', dependingOnBeforeValidation) |
| 60 | + .on('beforeValidation', dependingOnBeforeValidation) |
| 61 | + .each(function() { |
| 62 | + // Remove validation when on depending input |
| 63 | + var $dependingInput = $(this); |
| 64 | + $form.find('[name="'+$dependingInput.valAttr('depends-on')+'"]').each(function() { |
| 65 | + this.$dependingInput = $dependingInput; |
| 66 | + $(this) |
| 67 | + .off('change', dependingOnValueChanged) |
| 68 | + .on('change', dependingOnValueChanged) |
| 69 | + .valAttr('depending-value', $dependingInput.valAttr('depends-on-value')); |
| 70 | + }) |
| 71 | + |
38 | 72 | });
|
39 | 73 |
|
40 | 74 | },
|
41 |
| - setupValidationIfAnswered = function() { |
| 75 | + setupValidationTogetherWith = function($form) { |
| 76 | + $form.find('[data-validation-optional-if-answered]') |
| 77 | + .on('beforeValidation', function() { |
| 78 | + var $input = $(this), |
| 79 | + dependingInputs = $input.valAttr('optional-if-optional-if-answered'), |
| 80 | + dependingInputsHasValue = false, |
| 81 | + thisInputHasAnswer = $.formUtils.getValue($input) ? true:false; |
| 82 | + |
| 83 | + if (!thisInputHasAnswer) { |
| 84 | + $.each($.split('dependingInputs'), function(inputName) { |
| 85 | + var $dependingInput = $form.find('[name="'+inputName+'"]'); |
| 86 | + dependingInputsHasValue = $.formUtils.getValue($dependingInput) ? true:false; |
| 87 | + if (dependingInputsHasValue) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + }); |
42 | 91 |
|
| 92 | + if (dependingInputsHasValue) { |
| 93 | + $input.valAttr('skipped', 1); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + }); |
43 | 98 | };
|
44 | 99 |
|
45 |
| - $.formUtils.$win.bind('validatorsLoaded formValidationSetup', function(evt, $form) { |
| 100 | + $.formUtils.$win.bind('validatorsLoaded formValidationSetup', function(evt, $form, conf) { |
46 | 101 | if( !$form ) {
|
47 | 102 | $form = $('form');
|
48 | 103 | }
|
49 |
| - setupValidationDependsOn($form); |
50 |
| - setupValidationIfAnswered($form); |
| 104 | + setupValidationDependsOn($form, conf); |
| 105 | + setupValidationTogetherWith($form); |
51 | 106 | });
|
52 | 107 |
|
53 | 108 | })(jQuery);
|
0 commit comments