Skip to content

Forms, Validation & Redirects

Inertia works with ProcessWire’s native $input->post, $session->CSRF, and $notices APIs so you never need to rewrite form handling or security checks.

Automatic JSON-to-POST Sync & CSRF Protection

Section titled “Automatic JSON-to-POST Sync & CSRF Protection”

When your frontend submits a form with useForm() or router.post():

  1. JSON payload sync (syncJsonToPost = true): Inertia parses incoming application/json bodies and populates ProcessWire’s $input->post, so $input->post->text('title') and $input->post->email('email') work identically to classic HTML forms.
  2. CSRF protection (autoShareCsrf = true): Inertia shares ProcessWire’s CSRF token under props.csrf and emits an XSRF-TOKEN cookie so the Inertia client sends X-XSRF-TOKEN automatically on mutating requests.

Always validate the token on state-changing requests using ProcessWire’s native $session->CSRF->validate().

Classic Form Submissions (backWithErrors, flash, redirect)

Section titled “Classic Form Submissions (backWithErrors, flash, redirect)”

Handle POST, PUT, PATCH, or DELETE requests directly in your template controller or URL hook, validate inputs with $sanitizer, and return backWithErrors() or redirect():

<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var Page $page */
/** @var WireInput $input */
/** @var Sanitizer $sanitizer */
/** @var Session $session */
$inertia = inertia();
if ($input->requestMethod('POST')) {
$session->CSRF->validate();
$name = $sanitizer->text($input->post('name'));
$email = $sanitizer->email($input->post('email'));
$errors = [];
if ($name === '') {
$errors['name'] = 'Please enter your name.';
}
if ($email === '') {
$errors['email'] = 'Please enter a valid email address.';
}
if ($errors !== []) {
return $inertia->backWithErrors($errors);
}
$inertia->flash('success', 'Your inquiry has been received.');
return $inertia->preserveFragment()->redirect($page->url);
}
return $page->inertiaRender('Contact/Form');

Inertia supports live field-by-field validation before a form is submitted. When the client sends Precognition: true headers, call $inertia->precognition() with your validator closure:

<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var WireInput $input */
/** @var Sanitizer $sanitizer */
$inertia = inertia();
$precognition = $inertia->precognition(function () use ($input, $sanitizer): array {
$errors = [];
$email = $sanitizer->email($input->post('email'));
if ($email === '') {
$errors['email'] = 'Enter a valid email address.';
}
return $errors;
});
if ($precognition !== null) {
return $precognition;
}

When validation passes on a Precognition request, Inertia returns 204 No Content with Precognition-Success: true. When validation fails, it returns 422 Unprocessable Entity with scoped field errors without executing your save logic.

For Inertia v3 useHttp() calls that expect a raw JSON response instead of a full page visit, use $inertia->json() and $inertia->validationErrors():

<?php
declare(strict_types=1);
namespace ProcessWire;
/** @var WireInput $input */
/** @var Sanitizer $sanitizer */
$inertia = inertia();
$code = strtoupper($sanitizer->text($input->post('promoCode')));
if ($code === '') {
return $inertia->validationErrors([
'promoCode' => 'Enter a promo code.',
]);
}
return $inertia->json([
'valid' => true,
'discountPercent' => 15,
]);

Redirects, External Locations & Flash Notices

Section titled “Redirects, External Locations & Flash Notices”
Helper HTTP Status & Header When to Use
$inertia->redirect($url) 303 See Other (or 409 with X-Inertia-Redirect on cross-context redirects) Standard post-submission redirect to another Inertia page
$inertia->location($url) 409 Conflict with X-Inertia-Location Redirecting to an external URL or a classic non-Inertia ProcessWire page
$inertia->preserveFragment() Preserves URL #hash across redirect Returning to a specific anchored section after form save
$inertia->flash($key, $message) Shared once via props.flash One-time confirmation or warning messages

When autoShareFlash is enabled, standard ProcessWire $this->message(), $this->warning(), and $this->error() notices are also forwarded automatically in props.flash.

  • History encryption: Enable Encrypt Browser History by Default in module settings (encryptHistory = true), or call $inertia->encryptHistory() on sensitive routes (such as account settings or checkout) and $inertia->clearHistory() on logout.
  • Demo Mode ($config->demo): When ProcessWire runs in demo mode, mutating CLI commands (inertia:install, inertia:types, inertia:ssr start|stop) refuse write operations automatically.

Next steps