Skip to content

Commit efecf16

Browse files
committed
docs: update docs for pattern logic function
1 parent b82ba5f commit efecf16

File tree

1 file changed

+12
-5
lines changed
  • packages/forms/experimental/src/api/validators

1 file changed

+12
-5
lines changed

packages/forms/experimental/src/api/validators/pattern.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.dev/license
77
*/
8+
89
import {computed} from '@angular/core';
910
import {aggregateProperty, property, validate} from '../logic';
1011
import {PATTERN} from '../property';
@@ -13,11 +14,16 @@ import {ValidationError} from '../validation_errors';
1314
import {BaseValidatorConfig} from './util';
1415

1516
/**
16-
* Validator allowing to validate a string against a pattern.
17+
* Binds a validator to the given path that requires the value to match a specific regex pattern.
18+
* This function can only be called on string paths.
19+
* In addition to binding a validator, this function adds `PATTERN` property to the field.
1720
*
18-
* @param path Path to the target field
19-
* @param pattern Regular expression to validate against.
20-
* @param config Optional, currently allows providing custom errors function.
21+
* @param path Path of the field to validate
22+
* @param pattern The RegExp pattern to match, or a LogicFn that returns the RegExp pattern.
23+
* @param config Optional, allows providing any of the following options:
24+
* - `error`: Custom validation error(s) to be used instead of the default `ValidationError.pattern(pattern)`
25+
* or a function that receives the `FieldContext` and returns custom validation error(s).
26+
* @template TPathKind The kind of path the logic is bound to (a root path, child path, or item of an array)
2127
*/
2228
export function pattern<TPathKind extends PathKind = PathKind.Root>(
2329
path: FieldPath<string, TPathKind>,
@@ -31,7 +37,8 @@ export function pattern<TPathKind extends PathKind = PathKind.Root>(
3137
validate(path, (ctx) => {
3238
const pattern = ctx.state.property(PATTERN_MEMO)!();
3339

34-
// A pattern validator should not fail on an empty value.
40+
// A pattern validator should not fail on an empty value. This matches the behavior of HTML's
41+
// built in `pattern` attribute.
3542
if (pattern === undefined || ctx.value() == null || ctx.value() === '') {
3643
return undefined;
3744
}

0 commit comments

Comments
 (0)