New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MinLength Validator treats zero value as zero length #23749
Comments
Why would you need a min length of 0? Isn't it the same as not having a MinLength Validator in the first place? |
It's the value that is 0, not the min length. If the min length is 1, and the value is 0, the length is reported as 0 when it should be 1, and validation fails. Its not really a big problem since min length 1 is the same as required. But the logic is wrong. |
I would like to fix that issue. |
Having this issue with reactive forms in Angular 5 If passing the value 0 to a form control with a validator of Validators.minLength(1), the form is invalid even though the value 0 should satisfy this minimum length as it is a character |
@ShaneMcGowan I recommend you use Validators.required instead of Validators.minLength(1) as it is functionally equivalent and works properly. |
@gtranter Thanks, realised that after a short while and felt a bit silly, but still feel this issue should be fixed as it is quite odd behavior |
+ using Validators.required instead of Validators.minLength(1). Known issue and more information about that is available here: angular/angular#23749
remove truthy statement for assessing value existence because: - it is already checked previously - evaluating truthy value of 0 resolves to false which is not the desired behaviour. all that we need is to check the length property of the value Closes angular#23749
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
angular/packages/forms/src/validators.ts
Line 140 in 0cb4f12
The statement
const length: number = control.value ? control.value.length : 0;
clearly returns zero for length if the value is zero. For numeric inputs, this is a problem.The text was updated successfully, but these errors were encountered: