Nested actions
On the product page, a shopper can type a quantity, select Set quantity, and then adjust it with + or −. All three controls update the same page property. The form supplies a new value, while the buttons use the last value the page rendered.
How a page action works
Suppose the page shows 1 and the shopper types 3 and submits the form.
- The Atom client sends a POST to
/products/42with the action namesetQuantity, the current page state, and the submittedquantity=3. - Atom creates a new
ProductPageand restores the previous quantity, 1, because the property has#[State]. - Atom binds the submitted
quantityfield to the property marked with#[FromBody]. The property now holds the integer 3. - Atom calls
get("42")to load the product for this request. The handler leaves the quantity alone. - Atom calls
setQuantity(). The action sees 3 and keeps the quantity at 1 or higher. - Atom renders the updated HTML and includes 3 in the new page state. The client applies the update and the quantity reads 3.
Define an action
The same property participates in both state restoration and form binding. The order matters because the submitted value takes effect after the previous value has been restored.
#[State]
#[FromBody]
public int $quantity = 1;
#[PageAction]
public function setQuantity(): void
{
$this->quantity = max(1, $this->quantity);
}
The #[PageAction] attribute exposes setQuantity() as a page action. Put checks that depend on the submitted value inside the action, after binding has happened. See the complete page class for the imports and both quantity actions.
Page Forms
The input's name matches the PHP property. The form's atom:submit names the action that runs on submission.
<form method="post" atom:submit="setQuantity">
<label for="quantity">Set quantity</label>
<input id="quantity" name="quantity" type="number" min="1" step="1"
value="{{ $this->quantity }}" required>
<button type="submit">Set quantity</button>
</form>
The + and − buttons are outside the form. Each sends its own action and the current page state. Since these requests omit the quantity field, Atom leaves the restored quantity in place before running the method. After submitting 3, press + for 4, then − to return to 3. A fresh visit starts at 1.
Nested form state and actions
When a group of fields and actions belongs to the same form, you can encapsulate them in a model that the page owns. Atom can restore that model's state between requests, bind submitted fields to it, and call actions on the model itself.
On the product page, ProductQuantityForm owns the quantity and the actions that set, increase, or decrease it. ProductPage still loads the product and exposes the form to the template. Shoppers use the same controls to set and adjust the quantity.
Create app/Pages/ProductQuantityForm.php.
<?php
namespace App\Pages;
use Atom\Page\PageAction;
final class ProductQuantityForm
{
public int $quantity = 1;
#[PageAction]
public function setQuantity(): void
{
$this->quantity = max(1, $this->quantity);
}
#[PageAction]
public function incrementQuantity(): void
{
++$this->quantity;
}
#[PageAction]
public function decrementQuantity(): void
{
$this->quantity = max(1, $this->quantity - 1);
}
}
Replace the page class with the version below. #[FormModel] lets Atom create the typed form and bind matching fields to its public properties. #[State] carries the form's quantity into the next action request.
<?php
namespace App\Pages;
use App\Catalog\Product;
use App\Catalog\ProductService;
use Atom\Page\FormModel;
use Atom\Page\PageRoute;
use Atom\Page\State;
#[PageRoute("/products/{id}")]
final class ProductPage extends AppPage
{
public Product $product;
#[State]
#[FormModel]
public ProductQuantityForm $quantityForm;
public function __construct(private readonly ProductService $products)
{
}
public function get(string $id): void
{
$this->product = $this->products->getById($id);
}
public function availabilityLabel(): string
{
return $this->product->inStock ? "In stock" : "Sold out";
}
}
Replace the adjacent template too. The field name stays quantity. In quantityForm.setQuantity, quantityForm is the page property and setQuantity is the method Atom calls on that object after binding the submitted value.
<h1>{{ $this->product->name }}</h1>
<p>{{ $this->availabilityLabel() }}</p>
<form method="post" atom:submit="quantityForm.setQuantity">
<label for="quantity">Quantity</label>
<input id="quantity" name="quantity" type="number" min="1" step="1"
value="{{ $this->quantityForm->quantity }}" required>
<button type="submit">Set quantity</button>
</form>
<div role="group" aria-label="Quantity controls">
<button type="button" atom:action="quantityForm.decrementQuantity" aria-label="Decrease quantity">−</button>
<output aria-label="Quantity" aria-live="polite">{{ $this->quantityForm->quantity }}</output>
<button type="button" atom:action="quantityForm.incrementQuantity" aria-label="Increase quantity">+</button>
</div>
This model can back a reusable quantity control. The control can render a number input and step buttons from ProductQuantityForm::quantity and use the nested action names to set, increase, or decrease the value. The form model keeps the rules for changing the quantity in one place.