Core\Request::formFields
Core\Request::formFields(string $name): array<tainted string>Core\Request::formFields( string $name, ): array<tainted string>
Returns every value of a form field that has square brackets. You give the name without the brackets: for item[]=notebook&item[]=pen, the name is item, and the result is ["notebook", "pen"]. Each value is a tainted string, which is text that came from the client.
Empty brackets give a list. Brackets with a key give that key: size[shirt]=M gives ["shirt" => "M"]. A name without brackets gives a list with its one value. A field that was not sent gives an empty array, so you can loop over the result without a check for null.
formFields reads the same forms as Core\Request::formField. If you also need the uploaded files, call Core\Request::files() first. A name with two pairs of brackets throws a ParseError. Read those values with Core\Request::formFieldsAs. A command-line program has no request, so formFields throws a LogicError there.
The examples below read an order form, read fields with a key in the brackets, and show a single value and a missing field.
Parameters
$namestringThe field's name without the brackets. Foritem[]=pen, the name isitem.
Return value
Returns array<tainted string>. An array<tainted string>. item[]=pen&item[]=book gives ["pen", "book"], and size[shirt]=M gives ["shirt" => "M"]. A name without brackets gives an array with its one value. A name that is not in the form gives an empty array. Like formField, this method reads the body to its end, so call it after files().
Errors
LogicErrorThis program is not handling a request, orbodyorbodyStreamalready read the body of this request.ParseErrorThe body is not the form it says it is, or a value under this name has a second pair of brackets, such asitem[a][b]=c. Read nested values withformFieldsAs.RuntimeErrorThe name or value of a field is not valid UTF-8. In an urlencoded form, this includes a percent escape that decodes to bytes that are not UTF-8.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. |
Examples
An order form with several items
<?nvs// Shows how a shop reads an order form where the customer ticked several products.
// The form sends `item[]=notebook&item[]=pen`.// `formFields` returns every value of "item" as one list.array<tainted string> $items = Core\Request::formFields('item');
// This prints "notebook" and "pen", and then "2 items".foreach ($items as tainted string $item) { echo $item, "\n";}echo Core\Arr::count($items), " items\n";notebookpen2 itemsSizes for each product
<?nvs// Shows how to read form fields whose brackets contain a key, such as `size[shirt]=M`.
// The form sends `size[shirt]=M&size[hat]=L`, so `formFields` returns the keys "shirt" and "hat".// This prints "shirt: M" and "hat: L".foreach (Core\Request::formFields('size') as tainted string $product => tainted string $size) { echo $product, ': ', $size, "\n";}shirt: Mhat: LA single item and a missing field
<?nvs// Shows what `Core\Request::formFields` returns for a field without brackets and for a missing field.
// The form sends `item=pen¬e=gift+wrap`. "item" has no brackets.// The result is a list with its one value. This prints "items: 1".echo 'items: ', Core\Arr::count(Core\Request::formFields('item')), "\n";
// The form has no field "coupon", so the result is an empty array. This prints "coupons: 0".echo 'coupons: ', Core\Arr::count(Core\Request::formFields('coupon')), "\n";items: 1coupons: 0