From 8c54f3457ec2d954755bcec2b1a75072062f27a4 Mon Sep 17 00:00:00 2001 From: Wojt Janowski Date: Sun, 15 Nov 2026 02:31:30 +0000 Subject: [PATCH] Validation: listeners, polls and back are covered; wildcard validateOnly() Amp-Thread-ID: https://ampcode.com/threads/T-01a1dca0-9fd9-70ee-9a18-d0b2c7791ec9 Co-authored-by: Wojt Janowski --- .../mobile/4/digging-deeper/validation.md | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/resources/views/docs/mobile/4/digging-deeper/validation.md b/resources/views/docs/mobile/4/digging-deeper/validation.md index ab288f79..59b6eb0e 100644 --- a/resources/views/docs/mobile/4/digging-deeper/validation.md +++ b/resources/views/docs/mobile/4/digging-deeper/validation.md @@ -149,8 +149,9 @@ $this->validate( @@ -221,32 +222,43 @@ The bag is also passed to partials the screen renders, so `@@error` works inside ## Which handlers are covered -Validation errors are kept in the bag and the screen re-renders only when they're thrown while the screen handles a -user interaction: + + +Validation errors are kept in the bag, and the screen re-renders, when they're thrown from one of the screen's +handlers: - An event handler in the view, such as `@press`, `@tap` or `@change`. - A [`native:model`](data-binding) sync, including the `updated{Property}()` hook it runs. +- An `#[On]` or `#[OnNative]` [event listener](../the-basics/events), or a closure registered with `->on()`. +- A handler that a child component's event runs, either through `#[On]` or through an `@event` attribute on the + child's tag. +- A `#[Poll]` method. +- `onBackPressed()`. A `ValidationException` thrown anywhere else is not caught, and it reaches the error overlay like any other -exception. That includes: +exception. That includes `mount()`, `onResume()` and the other [lifecycle hooks](lifecycle-hooks). In those places, +call `validate()` inside a `try`/`catch` block, or check the data yourself and use `addError()`. + +In 4.7 and 4.8, only the first two kinds were covered. Event listeners, `#[Poll]` methods and `onBackPressed()` +reached the error overlay. -- `mount()`, `onResume()`, `onBackPressed()` and other [lifecycle hooks](lifecycle-hooks). -- `#[On]` and `#[OnNative]` [event listeners](../the-basics/events). -- `#[Poll]` methods. +### Child components -In those places, call `validate()` inside a `try`/`catch` block, or check the data yourself and use `addError()`. +Each handler is covered by the component it belongs to. When a child calls `emit()` and the parent's listener fails +validation, the messages go into the **parent's** bag, and the child's handler carries on after `emit()`. If the +parent doesn't use `ValidatesInput`, the error overlay appears, as it would for any other handler of that parent. ## Testing -[Interactions in tests](../testing/interactions) go through the same entry point as taps on a device, so a failed -validation in a test leaves its messages in the bag and re-renders the screen, just as it does in the app. You can +[Interactions in tests](../testing/interactions) go through the same handlers as on a device, so a failed validation +in a test leaves its messages in the bag and re-renders the screen, just as it does in the app. That includes +`emitNative()`, `firePoll()` and `pressBack()`. You can assert on the error text in the rendered screen, or read `getErrorBag()` from the component.