Skip to content

Commit 880333c

Browse files
authored
fix(formvalidation): support file inputs for validation
This PR reverts #1950 as the cause of the original issue was related to the separate dirty events which were refactored before 2.8.8 was released. I also added some checks for reset and set values as file inputs would otherwise return a js error as file input cannot be set any value other than an empty string. Additionally this PR simplifies the input change event detection as we dont support older browsers than ie11 and avoid memory leaks when attachEvents behavior is used and the form gets detroyed.
1 parent e19bdc5 commit 880333c

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

src/definitions/behaviors/form.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
$.fn.form = function (parameters) {
2323
var
2424
$allModules = $(this),
25+
$window = $(window),
2526

2627
time = Date.now(),
2728
performance = [],
@@ -60,6 +61,8 @@
6061
namespace,
6162
moduleNamespace,
6263
eventNamespace,
64+
attachEventsSelector,
65+
attachEventsAction,
6366

6467
submitting = false,
6568
dirty = false,
@@ -142,6 +145,9 @@
142145
module[action]();
143146
event.preventDefault();
144147
});
148+
149+
attachEventsSelector = selector;
150+
attachEventsAction = action;
145151
},
146152

147153
bindEvents: function () {
@@ -167,7 +173,7 @@
167173

168174
// Dirty events
169175
if (settings.preventLeaving) {
170-
$(window).on('beforeunload' + eventNamespace, module.event.beforeUnload);
176+
$window.on('beforeunload' + eventNamespace, module.event.beforeUnload);
171177
}
172178

173179
$field.on('change' + eventNamespace
@@ -185,6 +191,9 @@
185191
$module.on('clean' + eventNamespace, function (e) {
186192
settings.onClean.call();
187193
});
194+
if (attachEventsSelector) {
195+
module.attachEvents(attachEventsSelector, attachEventsAction);
196+
}
188197
},
189198

190199
clear: function () {
@@ -233,6 +242,7 @@
233242
isCheckbox = $field.is(selector.checkbox),
234243
isDropdown = $element.is(selector.uiDropdown) && module.can.useElement('dropdown'),
235244
isCalendar = $calendar.length > 0 && module.can.useElement('calendar'),
245+
isFile = $field.is(selector.file),
236246
isErrored = $fieldGroup.hasClass(className.error)
237247
;
238248
if (defaultValue === undefined) {
@@ -253,7 +263,7 @@
253263
$calendar.calendar('set date', defaultValue);
254264
} else {
255265
module.verbose('Resetting field value', $field, defaultValue);
256-
$field.val(defaultValue);
266+
$field.val(isFile ? '' : defaultValue);
257267
}
258268
});
259269
module.remove.states();
@@ -389,6 +399,13 @@
389399
$module.off(eventNamespace);
390400
$field.off(eventNamespace);
391401
$submit.off(eventNamespace);
402+
if (settings.preventLeaving) {
403+
$window.off(eventNamespace);
404+
}
405+
if (attachEventsSelector) {
406+
$(attachEventsSelector).off(eventNamespace);
407+
attachEventsSelector = undefined;
408+
}
392409
},
393410

394411
event: {
@@ -490,18 +507,7 @@
490507
return rule.type;
491508
},
492509
changeEvent: function (type, $input) {
493-
if (type === 'checkbox' || type === 'radio' || type === 'hidden' || $input.is('select')) {
494-
return 'change';
495-
}
496-
497-
return module.get.inputEvent();
498-
},
499-
inputEvent: function () {
500-
return document.createElement('input').oninput !== undefined
501-
? 'input'
502-
: (document.createElement('input').onpropertychange !== undefined
503-
? 'propertychange'
504-
: 'keyup');
510+
return ['file', 'checkbox', 'radio', 'hidden'].indexOf(type) >= 0 || $input.is('select') ? 'change' : 'input';
505511
},
506512
fieldsFromShorthand: function (fields) {
507513
var
@@ -1106,6 +1112,7 @@
11061112
$field = module.get.field(key),
11071113
$element = $field.parent(),
11081114
$calendar = $field.closest(selector.uiCalendar),
1115+
isFile = $field.is(selector.file),
11091116
isMultiple = Array.isArray(value),
11101117
isCheckbox = $element.is(selector.uiCheckbox) && module.can.useElement('checkbox'),
11111118
isDropdown = $element.is(selector.uiDropdown) && module.can.useElement('dropdown'),
@@ -1148,7 +1155,7 @@
11481155
$calendar.calendar('set date', value);
11491156
} else {
11501157
module.verbose('Setting field value', value, $field);
1151-
$field.val(value);
1158+
$field.val(isFile ? '' : value);
11521159
}
11531160
}
11541161
});
@@ -1651,9 +1658,10 @@
16511658
selector: {
16521659
checkbox: 'input[type="checkbox"], input[type="radio"]',
16531660
clear: '.clear',
1654-
field: 'input:not(.search):not([type="file"]):not([type="reset"]):not([type="button"]):not([type="submit"]), textarea, select',
1661+
field: 'input:not(.search):not([type="reset"]):not([type="button"]):not([type="submit"]), textarea, select',
1662+
file: 'input[type="file"]',
16551663
group: '.field',
1656-
input: 'input:not([type="file"])',
1664+
input: 'input',
16571665
message: '.error.message',
16581666
prompt: '.prompt.label',
16591667
radio: 'input[type="radio"]',

0 commit comments

Comments
 (0)