|
534 | 534 | * Check validator |
535 | 535 | */ |
536 | 536 |
|
537 | | - (function() { |
538 | | - /** |
539 | | - * Click submit form, check the validity when submit |
540 | | - */ |
541 | | - scope.$on(ctrl.$name + 'submit-' + uid, function(event, index) { |
542 | | - var value = ctrl.$viewValue, |
543 | | - isValid = false; |
544 | | - |
545 | | - isValid = checkValidation(scope, element, attrs, ctrl, validation, value); |
546 | | - |
547 | | - if (attrs.validMethod === 'submit') { |
548 | | - watch(); // clear previous scope.$watch |
549 | | - watch = scope.$watch('model', function(value, oldValue) { |
550 | | - value = ctrl.$viewValue; |
551 | | - |
552 | | - // don't watch when init |
553 | | - if (value === oldValue) { |
554 | | - return; |
555 | | - } |
| 537 | + /** |
| 538 | + * Click submit form, check the validity when submit |
| 539 | + */ |
| 540 | + scope.$on(ctrl.$name + 'submit-' + uid, function(event, index) { |
| 541 | + var value = ctrl.$viewValue, |
| 542 | + isValid = false; |
556 | 543 |
|
557 | | - // scope.$watch will translate '' to undefined |
558 | | - // undefined/null will pass the required submit /^.+/ |
559 | | - // cause some error in this validation |
560 | | - if (value === undefined || value === null) { |
561 | | - value = ''; |
562 | | - } |
| 544 | + isValid = checkValidation(scope, element, attrs, ctrl, validation, value); |
563 | 545 |
|
564 | | - isValid = checkValidation(scope, element, attrs, ctrl, validation, value); |
565 | | - }); |
| 546 | + if (attrs.validMethod === 'submit') { |
| 547 | + watch(); // clear previous scope.$watch |
| 548 | + watch = scope.$watch('model', function(value, oldValue) { |
| 549 | + value = ctrl.$viewValue; |
566 | 550 |
|
| 551 | + // don't watch when init |
| 552 | + if (value === oldValue) { |
| 553 | + return; |
567 | 554 | } |
568 | 555 |
|
569 | | - var setFocus = function(isValid) { |
570 | | - if (isValid) { |
571 | | - delete focusElements[index]; |
572 | | - } else { |
573 | | - focusElements[index] = element[0]; |
| 556 | + // scope.$watch will translate '' to undefined |
| 557 | + // undefined/null will pass the required submit /^.+/ |
| 558 | + // cause some error in this validation |
| 559 | + if (value === undefined || value === null) { |
| 560 | + value = ''; |
| 561 | + } |
574 | 562 |
|
575 | | - $timeout(function() { |
576 | | - focusElements[Math.min.apply(null, Object.keys(focusElements))].focus(); |
577 | | - }, 0); |
578 | | - } |
579 | | - }; |
| 563 | + isValid = checkValidation(scope, element, attrs, ctrl, validation, value); |
| 564 | + }); |
580 | 565 |
|
581 | | - if (isValid.constructor === Object) isValid.then(setFocus); |
582 | | - else setFocus(isValid); |
583 | | - }); |
| 566 | + } |
584 | 567 |
|
585 | | - /** |
586 | | - * Validate blur method |
587 | | - */ |
588 | | - if (attrs.validMethod === 'blur') { |
589 | | - element.bind('blur', function() { |
590 | | - var value = ctrl.$viewValue; |
591 | | - scope.$apply(function() { |
592 | | - checkValidation(scope, element, attrs, ctrl, validation, value); |
593 | | - }); |
594 | | - }); |
| 568 | + var setFocus = function(isValid) { |
| 569 | + if (isValid) { |
| 570 | + delete focusElements[index]; |
| 571 | + } else { |
| 572 | + focusElements[index] = element[0]; |
595 | 573 |
|
596 | | - return; |
597 | | - } |
| 574 | + $timeout(function() { |
| 575 | + focusElements[Math.min.apply(null, Object.keys(focusElements))].focus(); |
| 576 | + }, 0); |
| 577 | + } |
| 578 | + }; |
598 | 579 |
|
599 | | - /** |
600 | | - * Validate submit & submit-only method |
601 | | - */ |
602 | | - if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') { |
603 | | - return; |
604 | | - } |
| 580 | + if (isValid.constructor === Object) isValid.then(setFocus); |
| 581 | + else setFocus(isValid); |
| 582 | + }); |
605 | 583 |
|
606 | | - /** |
607 | | - * Validate watch method |
608 | | - * This is the default method |
609 | | - */ |
610 | | - scope.$watch('model', function(value) { |
611 | | - value = ctrl.$viewValue; |
612 | | - /** |
613 | | - * dirty, pristine, viewValue control here |
614 | | - */ |
615 | | - if (ctrl.$pristine && ctrl.$viewValue) { |
616 | | - // has value when initial |
617 | | - ctrl.$setViewValue(ctrl.$viewValue); |
618 | | - } else if (ctrl.$pristine) { |
619 | | - // Don't validate form when the input is clean(pristine) |
620 | | - if (scope.messageId) |
621 | | - angular.element(document.querySelector('#' + scope.messageId)).html(''); |
622 | | - else |
623 | | - element.next().html(''); |
624 | | - return; |
625 | | - } |
626 | | - checkValidation(scope, element, attrs, ctrl, validation, value); |
| 584 | + /** |
| 585 | + * Validate blur method |
| 586 | + */ |
| 587 | + if (attrs.validMethod === 'blur') { |
| 588 | + element.bind('blur', function() { |
| 589 | + var value = ctrl.$viewValue; |
| 590 | + scope.$apply(function() { |
| 591 | + checkValidation(scope, element, attrs, ctrl, validation, value); |
627 | 592 | }); |
| 593 | + }); |
| 594 | + |
| 595 | + return; |
| 596 | + } |
| 597 | + |
| 598 | + /** |
| 599 | + * Validate submit & submit-only method |
| 600 | + */ |
| 601 | + if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') { |
| 602 | + return; |
| 603 | + } |
628 | 604 |
|
629 | | - })(); |
| 605 | + /** |
| 606 | + * Validate watch method |
| 607 | + * This is the default method |
| 608 | + */ |
| 609 | + scope.$watch('model', function(value) { |
| 610 | + value = ctrl.$viewValue; |
| 611 | + /** |
| 612 | + * dirty, pristine, viewValue control here |
| 613 | + */ |
| 614 | + if (ctrl.$pristine && ctrl.$viewValue) { |
| 615 | + // has value when initial |
| 616 | + ctrl.$setViewValue(ctrl.$viewValue); |
| 617 | + } else if (ctrl.$pristine) { |
| 618 | + // Don't validate form when the input is clean(pristine) |
| 619 | + if (scope.messageId) |
| 620 | + angular.element(document.querySelector('#' + scope.messageId)).html(''); |
| 621 | + else |
| 622 | + element.next().html(''); |
| 623 | + return; |
| 624 | + } |
| 625 | + checkValidation(scope, element, attrs, ctrl, validation, value); |
| 626 | + }); |
630 | 627 |
|
631 | 628 | $timeout(function() { |
632 | 629 | /** |
|
0 commit comments