angular / angular/angular

Accept custom error message in Validators.pattern()

Open
#51,517 0 comments 2 reactions 0 assignees View on GitHub
area: forms forms: validators
Dominant language
TypeScript
Stars
101k
Forks
27.5k
Avg merge
1d 19h
Merged PRs (30d)
288

Description

### Which @angular/* package(s) are relevant/related to the feature request?

forms

### Description

Currently, `Validators.pattern()` is defined as so:

```
static pattern(pattern: string | RegExp): ValidatorFn;
```
When used and the control is invalid we get this as result:

```
pattern: {
requiredPattern: '/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,32}$/',
actualValue: 'hello'
}
```

Right now we can't assign a specific error message to that validator and we always need to use controlName + error type to display the error for the users.

### Proposed solution

The proposed solution is to accept a secondary parameter, with a custom error message and return that message along with the rest of the error data:

```
static pattern(pattern: string | RegExp, message: string | undefined): ValidatorFn;
```

### Alternatives considered

Perhaps this would be beneficial as well to be added to other types of Validators.

Contributor guide

Open the contributing guide

Research direction

Start in the Angular forms package at the Validators.pattern() entry point and inspect how its validation error data is assembled. Compare related validator APIs and their tests, then verify that the proposed custom message is returned with the existing pattern error data without changing current behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.