This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Form

A native form element with consolidated error handling.

This example sends events to the server: PHP runs in your browser.

The onSubmit handler of a Form runs on the server, as a Server Function. The browser does not send the form: the submit is an event of the page, and the handler gets it as its Event $e argument.

$e->form has the values of the form as an array. The key is the name of each control. The value is a string, or a list of strings when many controls have the same name. Read a value with a default, as in $e->form['username'] ?? ''.

The handler sets the state of the component, and its errors show in the FieldError of each field. While the request runs, phem locks the submit button with aria-disabled="true". To get the values as a typed object, see Validate with #[MapForm].

Examples#

Validate with #[MapForm]#

Add #[MapForm] to a parameter of the handler to get the values of the form as an object. The Validator checks its #[Assert] constraints before the handler runs, and the errors go to the Validation property of the component.

Show the error of each field with validation.error() in its FieldError. The field of an error is the property path of the object, so name each control as its property.

This example sends events to the server: PHP runs in your browser.

API reference#

Import the components of the family:

use Phem\Ui\Field;
use function Phem\Ui\{FieldLabel, FieldControl, FieldDescription, FieldError, FieldItem, Form};

Form#

A form that validates its fields before its submit: Base UI Form, on the phem-form controller. An invalid form does not send its onSubmit event, and the first invalid control gets the focus.

PropTypeDefault
validationMode?stringnull
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/Form.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close