fix(forms): disable PatternValidator gracefully for invalid input types#68257
Open
damiansire wants to merge 1 commit intoangular:mainfrom
Open
fix(forms): disable PatternValidator gracefully for invalid input types#68257damiansire wants to merge 1 commit intoangular:mainfrom
damiansire wants to merge 1 commit intoangular:mainfrom
Conversation
When using Signal Forms (`[formField]`) with a ControlValueAccessor that exposes an `@Input() pattern`, the framework's internal bindings loop propagates an empty array (`[]`) to the input when no pattern is defined on the field. Previously, this caused the legacy `PatternValidator` to crash with `TypeError: regex.test is not a function` because it attempted to execute `[].test(value)`. This commit improves `PatternValidator` by overriding the `enabled()` method to explicitly require a `string` or `RegExp`. Invalid input types will now gracefully disable the validator rather than causing a runtime crash. Additionally, `patternValidator()` adds a defensive type guard for further stability. Closes angular#68246
Contributor
|
@alxhub Requesting your expertise since you recently handled |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using Signal Forms (
[formField]) with a ControlValueAccessor that exposes an@Input() pattern, the framework's internal bindings loop propagates an empty array ([]) to the input when no pattern is defined on the field.Previously, this caused the legacy
PatternValidatorto crash withTypeError: regex.test is not a functionbecause it attempted to execute[].test(value).This commit improves
PatternValidatorby overriding theenabled()method to explicitly require astringorRegExp. Invalid input types will now gracefully disable the validator rather than causing a runtime crash. Additionally,patternValidator()adds a defensive type guard for further stability.Closes #68246
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When a control value accessor is used with
[formField]and provides a custompatterninput internally, Signal Forms propagates an empty array binding ([]) to the control. ThePatternValidatorblindly consumes this non-string array and crashes the entire application throwing aTypeError: regex.test is not a functionwhen it tries to run the validation check.Issue Number: #68246
What is the new behavior?
The
PatternValidatorlogic incorporates anenabled()method override to selectively intercept inputs (similar to whatRequiredValidatorandEmailValidatorcurrently do). If the bound pattern type isn't a string or a RegExp, the validator smoothly disables itself and returnsnullValidatorinstead of executing and causing a runtime exception.Does this PR introduce a breaking change?