Sub topic 7
Blocking navigation with validate()
This chapter assumes you can write a basic LWC with @api properties, build a simple screen flow in Flow Builder, and dispatch custom events. It builds on the previous chapters, where the component already receives inputs and sends outputs with FlowAttributeChangeEvent.
Why validate() exists
Flow Builder can only validate what it knows about. A required standard field blocks navigation on its own. A custom component has its own internal rules, and the flow can't see them. If a user types -3 into a quantity picker and clicks Next, the flow moves on unless the component tells it not to.
The validate() method is the hook for that. You expose it with @api, and the flow runtime calls it when the user tries to move forward.
When the flow calls validate()
The flow calls validate() on every component on the screen when the user clicks Next or Finish. It does not call it for Back or Pause, so users can leave a screen with incomplete data. The call is also not triggered while the user types. Typing triggers your own onchange handler, not the validation.
The method returns an object with two keys:
| Key | Type | Meaning |
|---|---|---|
| `isValid` | Boolean | `true` lets the flow continue. `false` keeps the user on the screen. |
| `errorMessage` | String | Text shown to the user when `isValid` is `false`. |
Implementing validate()
Here is the quantity picker from earlier chapters, with validation added.
import { LightningElement, api } from 'lwc';
import { FlowAttributeChangeEvent } from 'lightning/flowSupport';
export default class QuantityPicker extends LightningElement {
@api label = 'Quantity';
@api quantity;
handleChange(event) {
const parsed = parseInt(event.detail.value, 10);
this.quantity = Number.isNaN(parsed) ? undefined : parsed;
this.dispatchEvent(new FlowAttributeChangeEvent('quantity', this.quantity));
}
@api validate() {
if (Number.isInteger(this.quantity) && this.quantity > 0) {
return { isValid: true };
}
return {
isValid: false,
errorMessage: 'Enter a whole number greater than zero.'
};
}
}The rule is a whole number greater than zero. An empty field leaves quantity as undefined, so Number.isInteger returns false and the user sees the error message. The logic reads the component's own state, so it works even if the user never touched the field.
Common errors
- Forgetting
@api. Without it, the flow can't find the method and silently skips validation. The user advances with bad data. - Returning nothing or a bare Boolean. The flow expects an object with
isValid. Return{ isValid: true }explicitly. - Omitting
errorMessagewhen invalid. The flow still blocks navigation, but the user gets a generic message with no hint about the fix. - Validating only in
onchange. A user who never edits the field never triggers the handler. Put the rule invalidate()so it runs at navigation time. - Doing asynchronous work.
validate()must return its result synchronously. Don't return a Promise or call Apex inside it. Check the data you already hold, and do server checks in the flow itself, for example with a decision element after the screen.
Combining with inline messages
Validation on navigation tells users something is wrong only after they click. You can improve this by also calling reportValidity() on the lightning-input inside validate(). That highlights the field with its own error styling.
@api validate() {
const input = this.template.querySelector('lightning-input');
const valid = Number.isInteger(this.quantity) && this.quantity > 0;
if (!valid) {
input.setCustomValidity('Enter a whole number greater than zero.');
input.reportValidity();
return { isValid: false, errorMessage: 'Enter a whole number greater than zero.' };
}
input.setCustomValidity('');
input.reportValidity();
return { isValid: true };
}Clear the custom validity when the value becomes valid. Otherwise the field keeps its error even after the user corrects it.
Exercise
A user opens a screen with your component, leaves the field empty, and clicks Back. Does the flow call validate(), and why does this matter for your design?
Show answer
No. The flow calls validate() only for Next and Finish. Back and Pause skip it, so users can leave the screen with incomplete data. Your component should not assume that validate() has run before the flow reads its outputs, and any rule that must hold for downstream elements should be enforced on forward navigation.
Exercise
Why is it a mistake to call Apex inside validate() to check a value against the database?
Show answer
validate() must return its result synchronously. An Apex call is asynchronous, so the method can't return the real answer in time. Check data the component already holds, and run server-side checks in the flow after the screen, for example with a Get Records element and a Decision element.