close
    • Overview
@angular/forms/signals

required

function
stablesince v22.0

Binds a validator to the given path that requires the value to be non-empty. This function can only be called on any type of path. In addition to binding a validator, this function adds REQUIRED property to the field.

API

function required<TValue, TPathKind extends PathKind = PathKind.Root>(
  path: SchemaPath<TValue, 1, TPathKind>,
  config?:
    | (BaseValidatorConfig<TValue, TPathKind> & {
        when?: NoInfer<LogicFn<TValue, boolean, TPathKind>> | undefined;
      })
    | undefined,
): void;
@parampathSchemaPath<TValue, 1, TPathKind>

Path of the field to validate

@paramconfig(BaseValidatorConfig<TValue, TPathKind> & { when?: NoInfer<LogicFn<TValue, boolean, TPathKind>> | undefined; }) | undefined

Optional, allows providing any of the following options:

  • message: A user-facing message for the error.
  • error: Custom validation error(s) to be used instead of the default ValidationError.required() or a function that receives the FieldContext and returns custom validation error(s).
  • when: A function that receives the FieldContext and returns true if the field is required
@returnsvoid

Description

Binds a validator to the given path that requires the value to be non-empty. This function can only be called on any type of path. In addition to binding a validator, this function adds REQUIRED property to the field.

A value is considered empty when it is null, undefined, the empty string '', false, or NaN. Every other value is considered non-empty, including 0 and the empty array [] — use minLength() to require a minimum number of items in an array.

false is empty to follow the native semantics of required on <input type="checkbox">, where an unchecked box fails validation. NaN is empty because it is usually the result of a parsing error, and is not a valid number.

Jump to details