Skip to content
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

Closed
gtranter opened this issue May 7, 2018 · 8 comments
Closed

MinLength Validator treats zero value as zero length #23749

gtranter opened this issue May 7, 2018 · 8 comments
Labels
area: forms freq1: low help wanted An issue that is suitable for a community contributor (based on its complexity/scope). type: bug/fix
Milestone

Comments

@gtranter
Copy link

gtranter commented May 7, 2018

const length: number = control.value ? control.value.length : 0;

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.

@EmaGht
Copy link

EmaGht commented May 7, 2018

Why would you need a min length of 0? Isn't it the same as not having a MinLength Validator in the first place?

@gtranter
Copy link
Author

gtranter commented May 7, 2018

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.

@ngbot ngbot bot added this to the needsTriage milestone May 8, 2018
@kara kara added type: bug/fix help wanted An issue that is suitable for a community contributor (based on its complexity/scope). severity1: confusing freq1: low labels May 29, 2018
@ngbot ngbot bot modified the milestones: needsTriage, Backlog May 29, 2018
@adico1
Copy link

adico1 commented Jun 5, 2018

I would like to fix that issue.

@ShaneMcGowan
Copy link

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

@gtranter
Copy link
Author

@ShaneMcGowan I recommend you use Validators.required instead of Validators.minLength(1) as it is functionally equivalent and works properly.

@ShaneMcGowan
Copy link

@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

striky1 added a commit to striky1/material-community-components that referenced this issue Dec 19, 2018
+ using Validators.required instead of Validators.minLength(1).

Known issue and more information about that is available here: angular/angular#23749
ntsokos added a commit to ntsokos/angular that referenced this issue Mar 31, 2019
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
@AndrewKushnir
Copy link
Contributor

Thanks for reporting this issue @gtranter. This issue is also discussed in #35591, so I'm closing this ticket as a duplicate of #35591. Please subscribe to the #35591 to receive further updates. Thank you.

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators May 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: forms freq1: low help wanted An issue that is suitable for a community contributor (based on its complexity/scope). type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants