@@ -435,15 +435,29 @@ function validate(ctrl, validatorName, validity, value){
435435 return validity ? value : undefined ;
436436}
437437
438+ function testFlags ( validity , flags ) {
439+ var i , flag ;
440+ if ( flags ) {
441+ for ( i = 0 ; i < flags . length ; ++ i ) {
442+ flag = flags [ i ] ;
443+ if ( validity [ flag ] ) {
444+ return true ;
445+ }
446+ }
447+ }
448+ return false ;
449+ }
438450
439- function addNativeHtml5Validators ( ctrl , validatorName , element ) {
440- var validity = element . prop ( ' validity' ) ;
451+ // Pass validity so that behaviour can be mocked easier.
452+ function addNativeHtml5Validators ( ctrl , validatorName , badFlags , ignoreFlags , validity ) {
441453 if ( isObject ( validity ) ) {
454+ ctrl . $$hasNativeValidators = true ;
442455 var validator = function ( value ) {
443456 // Don't overwrite previous validation, don't consider valueMissing to apply (ng-required can
444457 // perform the required validation)
445- if ( ! ctrl . $error [ validatorName ] && ( validity . badInput || validity . customError ||
446- validity . typeMismatch ) && ! validity . valueMissing ) {
458+ if ( ! ctrl . $error [ validatorName ] &&
459+ ! testFlags ( validity , ignoreFlags ) &&
460+ testFlags ( validity , badFlags ) ) {
447461 ctrl . $setValidity ( validatorName , false ) ;
448462 return ;
449463 }
@@ -454,8 +468,9 @@ function addNativeHtml5Validators(ctrl, validatorName, element) {
454468}
455469
456470function textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
457- var validity = element . prop ( 'validity' ) ;
471+ var validity = element . prop ( VALIDITY_STATE_PROPERTY ) ;
458472 var placeholder = element [ 0 ] . placeholder , noevent = { } ;
473+ ctrl . $$validityState = validity ;
459474
460475 // In composition mode, users are still inputing intermediate text buffer,
461476 // hold the listener until composition is done.
@@ -493,11 +508,11 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
493508 value = trim ( value ) ;
494509 }
495510
496- if ( ctrl . $viewValue !== value ||
497- // If the value is still empty/falsy, and there is no `required` error, run validators
498- // again. This enables HTML5 constraint validation errors to affect Angular validation
499- // even when the first character entered causes an error.
500- ( validity && value === '' && ! validity . valueMissing ) ) {
511+ // If a control is suffering from bad input, browsers discard its value, so it may be
512+ // necessary to revalidate even if the control's value is the same empty value twice in
513+ // a row.
514+ var revalidate = validity && ctrl . $$hasNativeValidators ;
515+ if ( ctrl . $viewValue !== value || ( value === '' && revalidate ) ) {
501516 if ( scope . $$phase ) {
502517 ctrl . $setViewValue ( value ) ;
503518 } else {
@@ -603,6 +618,8 @@ function textInputType(scope, element, attr, ctrl, $sniffer, $browser) {
603618 }
604619}
605620
621+ var numberBadFlags = [ 'badInput' ] ;
622+
606623function numberInputType ( scope , element , attr , ctrl , $sniffer , $browser ) {
607624 textInputType ( scope , element , attr , ctrl , $sniffer , $browser ) ;
608625
@@ -617,7 +634,7 @@ function numberInputType(scope, element, attr, ctrl, $sniffer, $browser) {
617634 }
618635 } ) ;
619636
620- addNativeHtml5Validators ( ctrl , 'number' , element ) ;
637+ addNativeHtml5Validators ( ctrl , 'number' , numberBadFlags , null , ctrl . $$validityState ) ;
621638
622639 ctrl . $formatters . push ( function ( value ) {
623640 return ctrl . $isEmpty ( value ) ? '' : '' + value ;
0 commit comments