Skip to content

fix(forms): disable PatternValidator gracefully for invalid input types#68257

Open
damiansire wants to merge 1 commit intoangular:mainfrom
damiansire:fix/pattern-validator-array-crash
Open

fix(forms): disable PatternValidator gracefully for invalid input types#68257
damiansire wants to merge 1 commit intoangular:mainfrom
damiansire:fix/pattern-validator-array-crash

Conversation

@damiansire
Copy link
Copy Markdown
Contributor

@damiansire damiansire commented Apr 16, 2026

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 #68246

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

When a control value accessor is used with [formField] and provides a custom pattern input internally, Signal Forms propagates an empty array binding ([]) to the control. The PatternValidator blindly consumes this non-string array and crashes the entire application throwing a TypeError: regex.test is not a function when it tries to run the validation check.

Issue Number: #68246

What is the new behavior?

The PatternValidator logic incorporates an enabled() method override to selectively intercept inputs (similar to what RequiredValidator and EmailValidator currently do). If the bound pattern type isn't a string or a RegExp, the validator smoothly disables itself and returns nullValidator instead of executing and causing a runtime exception.

Does this PR introduce a breaking change?

  • Yes
  • No (I Think)

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
@pullapprove pullapprove bot requested a review from AndrewKushnir April 16, 2026 18:18
@ngbot ngbot bot added this to the Backlog milestone Apr 16, 2026
@JeanMeche JeanMeche requested review from leonsenft and removed request for AndrewKushnir April 16, 2026 18:23
@leonsenft leonsenft requested review from alxhub and removed request for leonsenft April 16, 2026 18:25
@leonsenft
Copy link
Copy Markdown
Contributor

@alxhub Requesting your expertise since you recently handled FormField / CVA compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using [formField] with a ControlValueAccessor that internally sets [pattern] is broken

2 participants