Skip to content

Angular's input validation should align with HTML5 validation! (Currently it does not!) #16352

Closed

Description

@tx8821

I'm submitting a ... (check one with "x")

[x] bug report
[x] feature request
[ ] support request

Current behavior

Suppose we have such an input:
<input #nativeControl #ngControl="ngModel" type="number" min="30">

Now if we enter 25 in that box, the ngControl.valid property will be true, while nativeControl.validity.valid will be false and nativeControl.validity.rangeUnderflow will be true.

Moreover, if we enter invalid characters (e.g. only dashes) in such a field, ngControl.valid property will be true. At the same time, nativeControl.validity.valid will be false and nativeControl.validity.badInput will be true;

Another example: <input type="email"> is not validated by Angular (i.e. is always valid), while HTML5 validation correctly sets ValidityState.typeMismatch to true (and ValidityState.valid to false) if invalid email address is entered.

Currently I'm testing this with chrome but I don't think there will be any differences with other browsers. They all implement HTML5 validation pretty much without any issues.

Expected behavior

I think HTML5 standard already implements validation mechanism pretty well. Please view https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation and https://developer.mozilla.org/en-US/docs/Web/API/ValidityState for example. I think Angular should either emulate, or even directly depend on that mechanism (ValidityState values defined by HTML5).

Yes I know one can implement custom validator in angular and basically emulate anything, but... I think I'd rather have <input type="email"> validation out of the box, rather than to reinvent the wheel and write new validator just for email checking.

[EDIT] Minimal reproduction of the problem with instructions
Please view an example: http://plnkr.co/edit/5pzh5pV2ExF3vKGhgEtq?p=info

Just enter number in first input, or email in second input, and you will see that Angular's validator does not respect min attribute (of <input type="number">) and also <input type="email"> inputs.

  • Angular version: 4.0.X
  • Browser: [all]
  • Language: [TypeScript 2.X]

Activity

  1. zoechi commented on Apr 26, 2017

    @zoechi
    Contributor

    Please provide a Plunker that demonstrates the issue.

  2. devoto13 commented on Apr 26, 2017

    @devoto13
    Contributor

    Note that Angular does have email validator.

  3. buu700 commented on Apr 27, 2017

    @buu700

    The answer I got on #15531 might possibly fix your problem.

  4. tx8821 commented on Apr 27, 2017

    @tx8821
    Author

    @zoechi Please view the Plunker at: http://plnkr.co/edit/5pzh5pV2ExF3vKGhgEtq?p=info
    Just enter number in first input, or email in second input, and you will see that Angular's validator does not respect min attribute (of <input type="number">) and also <input type="email"> inputs.

    @buu700 Unfortunately it is not relevant in this case.

  5. SilvioAmaral commented on Nov 17, 2017

    @SilvioAmaral

    I found out that the template driven forms were not working out-of-the-box for min and max, but were working perfectly for reactive driven forms.

    Example for reactive driven:
    https://stackblitz.com/edit/angular-form-reactive-min-max

    Template driven:
    https://stackblitz.com/edit/angular-template-min-max-not-there

    To my surprise, the min and max validators were created, but the directives weren't.
    https://github.com/angular/angular/blob/5.0.1/packages/forms/src/validators.ts#L46-L195
    https://github.com/angular/angular/blob/5.0.2/packages/forms/src/directives/validators.ts#L242-L278

    I added two directives to my project to fill in this gap, they were based on this project :
    https://github.com/czeckd/angular-minmax-validators (thanks )

    I can create a PR to angular if there is interest, but I wanted to ask first if this is something intentional or not.

    best regards

  6. Maximaximum commented on Dec 13, 2017

    @Maximaximum

    This issue explains the reasons #17491

  7. added
    feature
    on Dec 19, 2017
  8. added this to the Backlog milestone on Jan 23, 2018
  9. Karasuni commented on Feb 14, 2018

    @Karasuni

    @SilvioAmaral Please create the PR as this feature will otherwise stay in the backlog for a while

  10. SilvioAmaral commented on Feb 17, 2018

    @SilvioAmaral

    hey @Karasuni , I tried to push my commit to my branch and it was denied. Since I just signed the contributor agreement, i'm thinking this is the cause. Do I have to talk to anyone about it or just wait ? Sorry, first time contributing.

  11. devoto13 commented on Feb 17, 2018

    @devoto13
    Contributor

    @SilvioAmaral Make sure to read about forks. You should create one, than push your branch to your forked repo and than create a PR following this guide.

  12. trotyl commented on Feb 17, 2018

    @trotyl
    Contributor

    The fact is that min/max validator directives has been reverted, so if Angular team wants it back, just a "Revert: Revert: " would be fine, the commit is already there and no need for extra commits (and also it won't help).

    This is not something about workload.

  13. SilvioAmaral commented on Feb 17, 2018

    @SilvioAmaral

    @trotyl got it. not going to push it then. But why would you say it won't help ?

  14. Karasuni commented on Feb 17, 2018

    @Karasuni

    Is there no proposal to put it back then? Even if it is under a different name? I was certain I read somewhere it was OK to add this on to the html validators

  15. 22 remaining items

  16. darielbarroso commented on May 28, 2020

    @darielbarroso

    Sounds like people want to use their own min\max directives in their components that implement ControlValueAccessor. I think that is a valid request and that template driven form directives for min\max should really only apply to input fields and not any other component. It seems as though the original implementation used the following selector:

    [min][formControlName],[min][formControl],[min][ngModel]

    @Maximaximum mentioned that this should be this selector instead:

    [formControlName],input[type=number][min][formControl],input[type=number][min][ngModel]

    I think this is the ideal solution to not interfere with custom components while still providing the default assumption that html5 validation can be used with input fields out of the box.

  17. J5cott commented on Nov 13, 2020

    @J5cott

    I would like to be able to limit the possible values that the user can enter into my input. It would be nice if I could do this with just <input type="number" > on my reactive forms but this does not seem to work in Firefox (I encountered this problem with version 82.0.3). Firefox allows the user to enter non numeric characters. When this happens, the ValidityState.valid is false while the FormControl.valid is true.

    This may cause a situation where the user can enter a value to the input thinking it is ok but the FormControl does not receive the value and no feedback is given to the user indicating that they need to change their input.

    Until this bug is fixed here is a workaround that worked for me

  18. AndrewKushnir commented on Jan 22, 2021

    @AndrewKushnir
    Contributor

    Hi, just a quick update: there is a PR #39063 (in progress), which implements min/max validators for <input type="number">. These validators should be available in the next major version of Angular (most likely v12).

  19. dariel312 commented on Jan 22, 2021

    @dariel312

    Almost 4 years later, thank you!!

  20. added a commit that references this issue on Feb 5, 2021
    6065d1f
  21. angular-automatic-lock-bot commented on Mar 11, 2021

    @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.

  22. locked and limited conversation to collaborators on Mar 11, 2021
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

    area: formsfeaturefreq2: medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions