Skip to content

Angular input validation not in sync with HTMLInputElement validity #26787

Closed

Description

@PRR24

I'm submitting a...


[X] Bug report  

Current behavior

Angular FormControl.valid is true even though HTMLInputElement.checkValidity() returns false

Expected behavior

Angular validity check should be equal or more restrictive than native one, eg

  • if HTMLInputElement is not valid, FormControl should always be not valid
  • if HTMLInputElement is valid, FormControl may be not valid because of additional validators.

Minimal reproduction of the problem with instructions

<form novalidate>
    <input #test #testModel="ngModel" type="number" name="test" ngModel>
    <div>Native: {{ test.checkValidity() }} Angular: {{ testModel.valid }}</div>
</form>

Enter "asdf" in the input
Expected: Native and Angular should both display false
Actual: Native displays false, Angular displays true

What is the motivation / use case for changing the behavior?

Consistent input validation. Currently entering "10,000,000" in numeric field renders to null value through ngModel (parseFloat) binding and user gets no error feedback.

Environment


Angular version: 4 .. 7

Browser:
- [X] Safari 12
- [X] Firefox 62

Activity

  1. PRR24 commented on Oct 28, 2018

    @PRR24
    Author

    To work around the issue, one can use better/different ControlValueAccessor. For simpler use cases, simple workaround would probably be sufficient:

    @Directive({
        selector: "input[type=number][ngModel]",
        providers: [
            { provide: NG_VALIDATORS, useExisting: forwardRef(() => NativeValidator), multi: true }
        ]
    })
    export class NativeValidator implements Validator {
    
        constructor(
            private element: ElementRef
        ) {
        }
    
        validate(c: AbstractControl): ValidationErrors {
            if (!this.element.nativeElement.checkValidity()) {
                return { native: true };
            }
            return null;
        }
    }
    
  2. thw0rted commented on Nov 13, 2018

    @thw0rted

    I'm still reading issues (there are a lot of them!) but I don't understand why this isn't built-in behavior. "Native element is invalid" should be an error case for all template form controls, out of the box, automatically. The exact example you posted -- where test.validity.valid is false but testModel.valid is true -- should never happen.

    Is there a good argument against adding a default validation to all NgModel.control instances exactly like this?

  3. thw0rted commented on Nov 13, 2018

    @thw0rted

    Also, trackbacks to #16352, #17491, #23635.

  4. angular-automatic-lock-bot commented on Sep 14, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  5. locked and limited conversation to collaborators on Sep 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions