Conditional Validation in Angular 21 Signal Forms: `when` vs `applyWhen`
dev.to·4h·
Discuss: DEV
🏔️Alpine.js
Preview
Report Post

Angular 21’s Signal Forms introduce a powerful, declarative approach to conditional validation that makes complex form logic much cleaner. Let’s break down the two main approaches: when and applyWhen.

The when Property: Single Rule Conditions

The when property allows you to conditionally apply a single validation rule based on other field values. Perfect for simple conditional requirements:

required(schemaPath.email, {
message: 'Email is required',
when: ({ valueOf }) => valueOf(schemaPath.sendViaEmail) === true,
});

Here, the email field is only required when the sendViaEmail checkbox is checked. The valueOf() function gives you reactive access to any field’s current value.

The applyWhen Function: Multiple Rule Conditions

When you need to apply…

Similar Posts

Loading similar posts...