|
77 | 77 | initialize: function () {
|
78 | 78 | // settings grabbed at run time
|
79 | 79 | module.get.settings();
|
| 80 | + $module.addClass(className.initial); |
80 | 81 | if (methodInvoked) {
|
81 | 82 | if (instance === undefined) {
|
82 | 83 | module.instantiate();
|
|
130 | 131 | module.bindEvents();
|
131 | 132 | },
|
132 | 133 |
|
133 |
| - submit: function () { |
| 134 | + submit: function (event) { |
134 | 135 | module.verbose('Submitting form', $module);
|
135 | 136 | submitting = true;
|
136 | 137 | $module.trigger('submit');
|
| 138 | + if (event) { |
| 139 | + event.preventDefault(); |
| 140 | + } |
137 | 141 | },
|
138 | 142 |
|
139 | 143 | attachEvents: function (selector, action) {
|
|
159 | 163 | .on('click' + eventNamespace, selector.reset, module.reset)
|
160 | 164 | .on('click' + eventNamespace, selector.clear, module.clear)
|
161 | 165 | ;
|
| 166 | + $field.on('invalid' + eventNamespace, module.event.field.invalid); |
162 | 167 | if (settings.keyboardShortcuts) {
|
163 | 168 | $module.on('keydown' + eventNamespace, selector.field, module.event.field.keydown);
|
164 | 169 | }
|
|
274 | 279 | var
|
275 | 280 | allValid = true
|
276 | 281 | ;
|
277 |
| - $.each(validation, function (fieldName, field) { |
278 |
| - if (!module.validate.field(field, fieldName, true)) { |
| 282 | + $field.each(function (index, el) { |
| 283 | + var $el = $(el), |
| 284 | + validation = module.get.validation($el) || {}, |
| 285 | + identifier = module.get.identifier(validation, $el) |
| 286 | + ; |
| 287 | + if (!module.validate.field(validation, identifier, true)) { |
279 | 288 | allValid = false;
|
280 | 289 | }
|
281 | 290 | });
|
|
431 | 440 | if (!event.ctrlKey && key === keyCode.enter && isInput && !isInDropdown && !isCheckbox) {
|
432 | 441 | if (!keyHeldDown) {
|
433 | 442 | $field.one('keyup' + eventNamespace, module.event.field.keyup);
|
434 |
| - module.submit(); |
| 443 | + module.submit(event); |
435 | 444 | module.debug('Enter pressed on input submitting form');
|
436 |
| - event.preventDefault(); |
437 | 445 | }
|
438 | 446 | keyHeldDown = true;
|
439 | 447 | }
|
440 | 448 | },
|
441 | 449 | keyup: function () {
|
442 | 450 | keyHeldDown = false;
|
443 | 451 | },
|
| 452 | + invalid: function (event) { |
| 453 | + event.preventDefault(); |
| 454 | + }, |
444 | 455 | blur: function (event) {
|
445 | 456 | var
|
446 | 457 | $field = $(this),
|
447 |
| - $fieldGroup = $field.closest($group), |
448 |
| - validationRules = module.get.validation($field) |
| 458 | + validationRules = module.get.validation($field) || {}, |
| 459 | + identifier = module.get.identifier(validationRules, $field) |
449 | 460 | ;
|
450 |
| - if (validationRules && (settings.on === 'blur' || ($fieldGroup.hasClass(className.error) && settings.revalidate))) { |
| 461 | + if (settings.on === 'blur' || (!$module.hasClass(className.initial) && settings.revalidate)) { |
451 | 462 | module.debug('Revalidating field', $field, validationRules);
|
452 |
| - module.validate.field(validationRules); |
| 463 | + module.validate.field(validationRules, identifier); |
453 | 464 | if (!settings.inline) {
|
454 | 465 | module.validate.form(false, true);
|
455 | 466 | }
|
|
458 | 469 | change: function (event) {
|
459 | 470 | var
|
460 | 471 | $field = $(this),
|
461 |
| - $fieldGroup = $field.closest($group), |
462 |
| - validationRules = module.get.validation($field) |
| 472 | + validationRules = module.get.validation($field) || {}, |
| 473 | + identifier = module.get.identifier(validationRules, $field) |
463 | 474 | ;
|
464 |
| - if (validationRules && (settings.on === 'change' || ($fieldGroup.hasClass(className.error) && settings.revalidate))) { |
| 475 | + if (settings.on === 'change' || (!$module.hasClass(className.initial) && settings.revalidate)) { |
465 | 476 | clearTimeout(module.timer);
|
466 | 477 | module.timer = setTimeout(function () {
|
467 | 478 | module.debug('Revalidating field', $field, validationRules);
|
468 |
| - module.validate.field(validationRules); |
| 479 | + module.validate.field(validationRules, identifier); |
469 | 480 | if (!settings.inline) {
|
470 | 481 | module.validate.form(false, true);
|
471 | 482 | }
|
|
531 | 542 |
|
532 | 543 | return fullFields;
|
533 | 544 | },
|
| 545 | + identifier: function (validation, $el) { |
| 546 | + return validation.identifier || $el.attr('id') || $el.attr('name') || $el.data(metadata.validate); |
| 547 | + }, |
534 | 548 | prompt: function (rule, field) {
|
535 | 549 | var
|
536 | 550 | ruleName = module.get.ruleName(rule),
|
|
974 | 988 | $message.empty();
|
975 | 989 | },
|
976 | 990 | states: function () {
|
977 |
| - $module.removeClass(className.error).removeClass(className.success); |
| 991 | + $module.removeClass(className.error).removeClass(className.success).addClass(className.initial); |
978 | 992 | if (!settings.inline) {
|
979 | 993 | module.remove.errors();
|
980 | 994 | }
|
|
1211 | 1225 | return rule.type === 'empty';
|
1212 | 1226 | }) !== 0
|
1213 | 1227 | : false,
|
1214 |
| - identifier = validation.identifier || $el.attr('id') || $el.attr('name') || $el.data(metadata.validate) |
| 1228 | + identifier = module.get.identifier(validation, $el) |
1215 | 1229 | ;
|
1216 | 1230 | if (isRequired && !isDisabled && !hasEmptyRule && identifier !== undefined) {
|
1217 | 1231 | if (isCheckbox) {
|
|
1243 | 1257 | if (keyHeldDown) {
|
1244 | 1258 | return false;
|
1245 | 1259 | }
|
1246 |
| - |
| 1260 | + $module.removeClass(className.initial); |
1247 | 1261 | // reset errors
|
1248 | 1262 | formErrors = [];
|
1249 | 1263 | if (module.determine.isValid()) {
|
|
1315 | 1329 | ? module.get.field(field.depends)
|
1316 | 1330 | : false,
|
1317 | 1331 | fieldValid = true,
|
1318 |
| - fieldErrors = [] |
| 1332 | + fieldErrors = [], |
| 1333 | + isDisabled = $field.filter(':not(:disabled)').length === 0, |
| 1334 | + validationMessage = $field[0].validationMessage |
1319 | 1335 | ;
|
1320 | 1336 | if (!field.identifier) {
|
1321 | 1337 | module.debug('Using field name as identifier', identifier);
|
1322 | 1338 | field.identifier = identifier;
|
1323 | 1339 | }
|
1324 |
| - var isDisabled = $field.filter(':not(:disabled)').length === 0; |
| 1340 | + if (validationMessage) { |
| 1341 | + module.debug('Field is natively invalid', identifier); |
| 1342 | + fieldErrors.push(validationMessage); |
| 1343 | + fieldValid = false; |
| 1344 | + if (showErrors) { |
| 1345 | + $field.closest($group).addClass(className.error); |
| 1346 | + } |
| 1347 | + } else if (showErrors) { |
| 1348 | + $field.closest($group).removeClass(className.error); |
| 1349 | + } |
1325 | 1350 | if (isDisabled) {
|
1326 | 1351 | module.debug('Field is disabled. Skipping', identifier);
|
1327 | 1352 | } else if (field.optional && module.is.blank($field)) {
|
1328 | 1353 | module.debug('Field is optional and blank. Skipping', identifier);
|
1329 | 1354 | } else if (field.depends && module.is.empty($dependsField)) {
|
1330 | 1355 | module.debug('Field depends on another value that is not present or empty. Skipping', $dependsField);
|
1331 | 1356 | } else if (field.rules !== undefined) {
|
1332 |
| - if (showErrors) { |
1333 |
| - $field.closest($group).removeClass(className.error); |
1334 |
| - } |
1335 | 1357 | $.each(field.rules, function (index, rule) {
|
1336 | 1358 | if (module.has.field(identifier)) {
|
1337 | 1359 | var invalidFields = module.validate.rule(field, rule, true) || [];
|
|
1348 | 1370 | }
|
1349 | 1371 | if (fieldValid) {
|
1350 | 1372 | if (showErrors) {
|
1351 |
| - module.remove.prompt(identifier, fieldErrors); |
| 1373 | + module.remove.prompt(identifier); |
1352 | 1374 | settings.onValid.call($field);
|
1353 | 1375 | }
|
1354 | 1376 | } else {
|
|
1638 | 1660 | isExactly: '{name} must be exactly "{ruleValue}"',
|
1639 | 1661 | not: '{name} cannot be set to "{ruleValue}"',
|
1640 | 1662 | notExactly: '{name} cannot be set to exactly "{ruleValue}"',
|
1641 |
| - contain: '{name} must contain "{ruleValue}"', |
1642 |
| - containExactly: '{name} must contain exactly "{ruleValue}"', |
| 1663 | + contains: '{name} must contain "{ruleValue}"', |
| 1664 | + containsExactly: '{name} must contain exactly "{ruleValue}"', |
1643 | 1665 | doesntContain: '{name} cannot contain "{ruleValue}"',
|
1644 | 1666 | doesntContainExactly: '{name} cannot contain exactly "{ruleValue}"',
|
1645 | 1667 | minLength: '{name} must be at least {ruleValue} characters',
|
|
1673 | 1695 | },
|
1674 | 1696 |
|
1675 | 1697 | className: {
|
| 1698 | + initial: 'initial', |
1676 | 1699 | error: 'error',
|
1677 | 1700 | label: 'ui basic red pointing prompt label',
|
1678 | 1701 | pressed: 'down',
|
|
0 commit comments