|
480 | 480 |
|
481 | 481 | case "required":
|
482 | 482 | required = true;
|
483 |
| - errorMsg = methods._required(field, rules, i, options); |
| 483 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._required); |
484 | 484 | break;
|
485 | 485 | case "custom":
|
486 |
| - errorMsg = methods._custom(field, rules, i, options); |
| 486 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._custom); |
487 | 487 | break;
|
488 | 488 | case "groupRequired":
|
489 | 489 | // Check is its the first of group, if not, reload validation with first field
|
|
494 | 494 | methods._validateField(firstOfGroup, options, skipAjaxValidation);
|
495 | 495 | options.showArrow = true;
|
496 | 496 | continue;
|
497 |
| - }; |
498 |
| - errorMsg = methods._groupRequired(field, rules, i, options); |
499 |
| - if(errorMsg) required = true; |
| 497 | + } |
| 498 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._groupRequired); |
| 499 | + if(errorMsg) required = true; |
500 | 500 | options.showArrow = false;
|
501 | 501 | break;
|
502 | 502 | case "ajax":
|
|
507 | 507 | }
|
508 | 508 | break;
|
509 | 509 | case "minSize":
|
510 |
| - errorMsg = methods._minSize(field, rules, i, options); |
| 510 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._minSize); |
511 | 511 | break;
|
512 | 512 | case "maxSize":
|
513 |
| - errorMsg = methods._maxSize(field, rules, i, options); |
| 513 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._maxSize); |
514 | 514 | break;
|
515 | 515 | case "min":
|
516 |
| - errorMsg = methods._min(field, rules, i, options); |
| 516 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._min); |
517 | 517 | break;
|
518 | 518 | case "max":
|
519 |
| - errorMsg = methods._max(field, rules, i, options); |
| 519 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._max); |
520 | 520 | break;
|
521 | 521 | case "past":
|
522 | 522 | errorMsg = methods._past(form, field, rules, i, options);
|
|
550 | 550 | options.showArrow = false;
|
551 | 551 | break;
|
552 | 552 | case "maxCheckbox":
|
553 |
| - errorMsg = methods._maxCheckbox(form, field, rules, i, options); |
| 553 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._maxCheckbox); |
554 | 554 | field = $(form.find("input[name='" + fieldName + "']"));
|
555 | 555 | break;
|
556 | 556 | case "minCheckbox":
|
557 |
| - errorMsg = methods._minCheckbox(form, field, rules, i, options); |
| 557 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._minCheckbox); |
558 | 558 | field = $(form.find("input[name='" + fieldName + "']"));
|
559 | 559 | break;
|
560 | 560 | case "equals":
|
561 |
| - errorMsg = methods._equals(field, rules, i, options); |
| 561 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._equals); |
562 | 562 | break;
|
563 | 563 | case "funcCall":
|
564 |
| - errorMsg = methods._funcCall(field, rules, i, options); |
| 564 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._funcCall); |
565 | 565 | break;
|
566 | 566 | case "creditCard":
|
567 |
| - errorMsg = methods._creditCard(field, rules, i, options); |
| 567 | + errorMsg = methods._getErrorMessage(form, field, rules[i], rules, i, options, methods._creditCard); |
568 | 568 | break;
|
569 | 569 |
|
570 | 570 | default:
|
|
587 | 587 | }
|
588 | 588 |
|
589 | 589 | if(field.is(":hidden") && options.prettySelect) {
|
590 |
| - field = form.find("#" + options.usePrefix + field.attr('id') + options.useSuffix); |
591 |
| - } |
| 590 | + field = form.find("#" + options.usePrefix + field.attr('id') + options.useSuffix); |
| 591 | + } |
592 | 592 |
|
593 | 593 | if (options.isError){
|
594 | 594 | methods._showPrompt(field, promptText, "", false, options);
|
|
611 | 611 |
|
612 | 612 | return options.isError;
|
613 | 613 | },
|
| 614 | + /******************** |
| 615 | + * _getErrorMessage |
| 616 | + * |
| 617 | + * @param form |
| 618 | + * @param field |
| 619 | + * @param rule |
| 620 | + * @param rules |
| 621 | + * @param i |
| 622 | + * @param options |
| 623 | + * @param originalValidationMethod |
| 624 | + * @return {*} |
| 625 | + * @private |
| 626 | + */ |
| 627 | + _getErrorMessage:function (form, field, rule, rules, i, options, originalValidationMethod) { |
| 628 | + // If we are using the custon validation type, build the index for the rule. |
| 629 | + // Otherwise if we are doing a function call, make the call and return the object |
| 630 | + // that is passed back. |
| 631 | + if (rule == "custom") { |
| 632 | + var custom_validation_type_index = rules.indexOf(rule) + 1; |
| 633 | + var custom_validation_type = rules[custom_validation_type_index]; |
| 634 | + rule = "custom[" + custom_validation_type + "]"; |
| 635 | + } |
| 636 | + var id = field.context.attributes.id.nodeValue; |
| 637 | + var element_classes = field.context.attributes['class'].nodeValue; |
| 638 | + var element_classes_array = element_classes.split(" "); |
| 639 | + var custom_message = methods._getCustomErrorMessage(id, element_classes_array, rule, options); |
| 640 | + |
| 641 | + // Call the original validation method. If we are dealing with dates, also pass the form |
| 642 | + var errorMsg; |
| 643 | + if (rule == "future" || rule == "past") { |
| 644 | + errorMsg = originalValidationMethod(form, field, rules, i, options); |
| 645 | + } else { |
| 646 | + errorMsg = originalValidationMethod(field, rules, i, options); |
| 647 | + } |
| 648 | + |
| 649 | + // If the original validation method returned an error and we have a custom error message, |
| 650 | + // return the custom message instead. Otherwise return the original error message. |
| 651 | + if (errorMsg != undefined && custom_message) { |
| 652 | + return custom_message; |
| 653 | + } |
| 654 | + return errorMsg; |
| 655 | + |
| 656 | + }, |
| 657 | + _getCustomErrorMessage:function (id, classes, rule, options) { |
| 658 | + var custom_message = false; |
| 659 | + id = '#' + id; |
| 660 | + // If we have custom messages for the element's id, get the message for the rule from the id. |
| 661 | + // Otherwise, if we have custom messages for the element's classes, use the first class message we find instead. |
| 662 | + if (typeof options.custom_error_messages[id] != "undefined" && |
| 663 | + typeof options.custom_error_messages[id][rule] != "undefined" ) { |
| 664 | + custom_message = options.custom_error_messages[id][rule]['message']; |
| 665 | + } else if (classes.length > 0) { |
| 666 | + for (var i = 0; i < classes.length && classes.length > 0; i++) { |
| 667 | + var element_class = "." + classes[i]; |
| 668 | + if (typeof options.custom_error_messages[element_class] != "undefined" && |
| 669 | + typeof options.custom_error_messages[element_class][rule] != "undefined") { |
| 670 | + custom_message = options.custom_error_messages[element_class][rule]['message']; |
| 671 | + break; |
| 672 | + } |
| 673 | + } |
| 674 | + } |
| 675 | + if (!custom_message && |
| 676 | + typeof options.custom_error_messages[rule] != "undefined" && |
| 677 | + typeof options.custom_error_messages[rule]['message'] != "undefined"){ |
| 678 | + custom_message = options.custom_error_messages[rule]['message']; |
| 679 | + } |
| 680 | + return custom_message; |
| 681 | + }, |
614 | 682 | /**
|
615 | 683 | * Required validation
|
616 | 684 | *
|
|
1633 | 1701 |
|
1634 | 1702 | // Used when you have a form fields too close and the errors messages are on top of other disturbing viewing messages
|
1635 | 1703 | doNotShowAllErrosOnSubmit: false,
|
| 1704 | + custom_error_messages:{}, |
1636 | 1705 |
|
1637 | 1706 | // true if you want to vind the input fields
|
1638 | 1707 | binded: true,
|
|
0 commit comments