Core\Request::formFieldsAs
Core\Request::formFieldsAs<T>({name?: string}): TCore\Request::formFieldsAs<T>( {name?: string}, ): T
Reads a form that a browser sent with POST into a class or a shape that you write. You write the type between < and >: Core\Request::formFieldsAs<SignUp>(). A class needs the #[Core\Json\Derive] attribute, and each public property reads the form field with the same name. The form comes from the client, so every string field must be tainted string. This also applies to a class inside a shape or an array, however deep it is.
Every value in a form is text. A field of another type, such as uint, converts the text the same way as does. Form fields that the type does not name are ignored. formFieldsAs reads both kinds of form: application/x-www-form-urlencoded and multipart/form-data.
The option name reads only the fields inside one bracket name. For address[city]=Paris, you write { name: "address" }, and the field city gets Paris.
If a field is missing or its value does not convert, formFieldsAs throws a ParseError. Its issues list has one entry for each wrong field.
The examples below read a sign-up form into a class and show the error for a wrong age. The last one reads the lines of an order form into a list.
Options
namestringoptionalThe field whose nested value is read, written without brackets. Brackets work the same way as informField. If you leave it out, the whole form is read.
Return value
Returns T. A new T with every field of its declared type, or one T for each entry for an array<T>. Like formField, this method reads the body to its end. On a multipart/form-data request, call it after the files() loop. Nothing is kept. Every call reads the form again, so two callers never get the same object.
Errors
LogicErrorThis program is not handling a request. OrbodyorbodyStreamalready read the body of this request. OrTis a class without#[Json\Derive].ParseErrorThe body is not the form it says it is, for the same reasons as informField. Or a field thatTneeds is missing, orascannot convert its value to the declared type. Or the value undernameis not a set of fields. The error has one issue for each wrong field, at its own dotted path.IOErrorThe connection failed while the body was read, or the body ended before the length it declared.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Request::queryParameter, queryParameters and queryParametersAs read the query string. formField, formFields and formFieldsAs read a submitted form. The old names were query, queryArray, queryAs, post, postArray and postAs. |
0.0.6 | Some decoders take a type, such as Core\Json::decodeAs and Core\Request::jsonAs. They check every class inside a shape or an array of that type, at any depth. Core\Request::jsonAs, queryParametersAs and formFieldsAs give the error E0810 when such a class has a plain string property. |
Examples
Reading a sign up form into a class
<?nvs// Shows how to read a submitted sign-up form into your own class.
// With `#[Core\Json\Derive]`, each public property reads the form field with the same name.// The form comes from the client, so a string property must be `tainted string`.#[Core\Json\Derive]class SignUp{ public tainted string $name; public tainted string $email; public uint $age;
public function constructor(tainted string $name, tainted string $email, uint $age) { $this->name = $name; $this->email = $email; $this->age = $age; }}
// The form is `name=Ada+Lovelace&email=ada%40example.com&age=36&newsletter=on`.// The text "36" is converted to the number 36. The field `newsletter` is ignored.var $form = Core\Request::formFieldsAs<SignUp>();
// This prints "Ada Lovelace <ada@example.com>, 36".echo $form->name, ' <', $form->email, '>, ', $form->age, "\n";Ada Lovelace <ada@example.com>, 36An age that is not a number
<?nvs// Shows what `Core\Request::formFieldsAs` does when a form field is missing or has the wrong type.
// The form is `name=Ada&age=young`. "young" is not a number, and there is no "email".// `formFieldsAs` checks every field and then throws one `ParseError`.try { var $form = Core\Request::formFieldsAs<tainted { name: string, email: string, age: uint }>(); echo 'welcome, ', $form->name, "\n";} catch (ParseError $error) { // The error's `issues` property has one entry for each wrong field. This prints two lines: // "age: expected uint, found a string" and "email: required field missing". foreach ($error->issues as { path: string, message: string } $issue) { echo $issue->path, ': ', $issue->message, "\n"; }}age: expected uint, found a stringemail: required field missingThe lines of an order form
<?nvs// Shows how a shop reads the lines of an order form into a list.
// The form sends `line[0][product]=notebook&line[0][quantity]=3` and one more line for a pen.// The option `name` reads only the fields inside `line[...]`, and each line becomes one shape.var $lines = Core\Request::formFieldsAs<array<tainted { product: string, quantity: uint }>>({ name: 'line' });
// This prints "3 x notebook", "5 x pen" and then "8 items".uint $count = 0;foreach ($lines as tainted { product: string, quantity: uint } $line) { echo $line->quantity, ' x ', $line->product, "\n"; $count = $count + $line->quantity;}echo $count, " items\n";3 x notebook5 x pen8 items