Repository navigation
Description
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
zoechi commentedon Apr 26, 2017
Please provide a Plunker that demonstrates the issue.
devoto13 commentedon Apr 26, 2017
Note that Angular does have email validator.
buu700 commentedon Apr 27, 2017
The answer I got on #15531 might possibly fix your problem.
tx8821 commentedon Apr 27, 2017
@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.
SilvioAmaral commentedon Nov 17, 2017
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
Maximaximum commentedon Dec 13, 2017
This issue explains the reasons #17491
Karasuni commentedon Feb 14, 2018
@SilvioAmaral Please create the PR as this feature will otherwise stay in the backlog for a while
SilvioAmaral commentedon Feb 17, 2018
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.
devoto13 commentedon Feb 17, 2018
@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.
trotyl commentedon Feb 17, 2018
The fact is that
min/maxvalidator 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.
SilvioAmaral commentedon Feb 17, 2018
@trotyl got it. not going to push it then. But why would you say it won't help ?
Karasuni commentedon Feb 17, 2018
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
22 remaining items
darielbarroso commentedon May 28, 2020
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.
J5cott commentedon Nov 13, 2020
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
AndrewKushnir commentedon Jan 22, 2021
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).dariel312 commentedon Jan 22, 2021
Almost 4 years later, thank you!!
feat(forms): introduce min and max validators
angular-automatic-lock-bot commentedon Mar 11, 2021
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.