You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
We've come across to what I think is a bug in form validation in reactive forms. Stackblitz is kind of self-explanatory, but to sum it up, when creating FormControl (that is not visible right away on the view), validation works differently depending on how validators are passed onto the control (simple validator vs array of validators).
If you open the stackblitz, and right away click on the buttons you'll see that one of the forms shows an alert and the other doesn't. The only difference between these two is the validatorOrOpts param used to create the controls.
Please provide a link to a minimal reproduction of the bug
Both forms should either be valid or invalid (based on the expected behaviour from the Angular team) instead of varying based on how the ValidatorFn are passed.
Please provide the environment you discovered this bug in (run ng version)
solves the issue, but looks like a random difference in validation :/
In our case we're using the first to return all the errors in one (as we have some complex validators that might need of all the errors to properly build feedback for the user).
The text was updated successfully, but these errors were encountered:
The root cause here is that when a control has an array of validators, they go through the compose function, and when there's only a single validator (not wrapped in an array) the compose code path is skipped:
As you found out, the compose path uses Object.keys().length to convert an empty object to null, which causes the array version to be treated as valid, and the non-array version is treated as invalid because it lacks that conversion.
I think the question here is whether an empty object should be:
invalid
valid
avoided
Currently it's both (1) and (2)
JeanMeche
added a commit
to JeanMeche/angular
that referenced
this issue
May 17, 2024
In some cases the validator could return an empty object while having all validators returning null
This commit makes it more consistant by treating empty error objects as valid controls.
fixesangular#54214
Which @angular/* package(s) are the source of the bug?
forms
Is this a regression?
No
Description
We've come across to what I think is a bug in form validation in reactive forms. Stackblitz is kind of self-explanatory, but to sum it up, when creating FormControl (that is not visible right away on the view), validation works differently depending on how validators are passed onto the control (simple validator vs array of validators).
If you open the stackblitz, and right away click on the buttons you'll see that one of the forms shows an alert and the other doesn't. The only difference between these two is the validatorOrOpts param used to create the controls.
Please provide a link to a minimal reproduction of the bug
https://stackblitz.com/edit/stackblitz-starters-378jka?file=src%2Fmain.ts
Please provide the exception or error you saw
Please provide the environment you discovered this bug in (run
ng version
)Anything else?
After further testing looks like changing the validator's return from
to
solves the issue, but looks like a random difference in validation :/
In our case we're using the first to return all the errors in one (as we have some complex validators that might need of all the errors to properly build feedback for the user).
The text was updated successfully, but these errors were encountered: