Skip to content

Possibility to configure Validators.required behavior or new Validators.strictlyRequired validator #53357

@liesahead

Description

@liesahead

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

forms

Description

Currently Validators.required allows the entry of a space character, making the form valid. Therefore, we need to use a custom required validator.

public static required({ value }: AbstractControl): ValidationErrors | null {
    return String(value ?? '').trim() ? null : { required: true };
}

However, this is causing problems with material form fields as the required asterisks are not displayed next to the label due to the failure of this check.
image

Proposed solution

Could you consider adding an option to pass a boolean flag (e.g., ignoreSpaces) during the initialization of this validator? Alternatively, you could introduce a new strictlyRequired validator to address this concern, allowing users to choose without modifying the existing one.

Alternatives considered

Currently we have to use this work-around simply to override required attribute value to display asterisks.

public get hasRequiredValidator(): boolean {
    return this.control.hasValidator(CmValidators.required) || this.control.hasValidator(Validators.required);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions