Skip to content

Simple async validation in signal forms #69779

Description

@JakobDev

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

forms

Description

I use a lot of custom validation in my App. The new Signal Forms provide an easy way to do that:

myForm = form(this.myModel, (schemaPath) => {
  validate(schemaPath.myField, this.validateCustom);
});

private validateCustom(state: ChildFieldContext<string>): ValidationResult {
    const value = state.value();

    if (value !== "foo") {
      return {
        kind: "test",
        message: "my message",
      };
    }

    return null;
}

I can just write a simple function which checks the value and returns null or an error.

However, the problems start when I want async validation. This is possible but according to the docs I need now a complex object with params, factory, onSuccess, and onError. So the simple sync function turns now into a nightmare with a lot of boilerplate code.

Proposed solution

Just the same simple function as for sync validation:

myForm = form(this.myModel, (schemaPath) => {
  validateSimpleAsync(schemaPath.myField, this.validateCustom);
});

private async validateCustom(state: ChildFieldContext<string>): Promise<ValidationResult> {
    const value = state.value();

    if (value !== "foo") {
      return {
        kind: "test",
        message: "my message",
      };
    }

    return null;
}

Alternatives considered

Write some wrapper myself

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status
    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions