Skip to content

Signal Forms: applyWhen with else option #69183

@paulstelzer

Description

@paulstelzer

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

@angular/forms/signals

Description

When a subform is based on another Selection we have to use disabled and applyWhen (see the next example). I found no other option for this use case.

So in this example the other decides for goal points. Then the pointsSchema (could be required counter or a min of 10) should be applied. If the user selects something else this should be disabled -> Validation will be disabled.

interface MyForm {
   goal: {
    decision: 'points' | 'cards';
  }
  points: {
    counter: number;
  }
}

export const mySchema = 
  schema<MyForm>((schema) => {
    const decision = schema.goal.decision;

    disabled(schema.points, {
      when: ({ valueOf }) => valueOf(decision) !== 'points',
    });
    applyWhen(
      schema.points,
      ({ valueOf }) => valueOf(decision) === 'points',
      pointsSchema,
    );
  });

Proposed solution

valueOf can be part of schema at the top or applyWhen should have an else part or can contain options (in this case to disable the schema)

Alternatives considered

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions