Typed field values
Each field type maps to an attributes AttributeType (see forms.field_types), so stored values read back as real PHP types instead of raw strings, and submitted values are type-checked during validation:
Forms::submissions($form)->first()->values;
// ['age' => 42, 'subscribed' => true, 'born' => CarbonImmutable, 'tags' => ['a', 'b']]
// One per-field row (a `Submission`) reads its own value the same way:
$row = $form->fields->firstWhere('key', 'age')->submissions()->first();
$row->typedValue(); // 42 (int); true (bool) for a `checkbox` rowDefault mappings
| Field types | AttributeType | Reads back as |
|---|---|---|
number, range | integer | int |
float, decimal | float | float |
checkbox, boolean, toggle | boolean | bool |
date, datetime | datetime | CarbonImmutable |
multiselect, checkboxes, tags | array | array |
time and anything else | string | exactly as stored |
Type checking
After Laravel’s rules pass, each visible, non-null value whose field maps to a non-string type is checked against that type. A mismatch fails Forms::validate() (and finalize) with a ValidationException under the field’s path, like any other rule — a decimal in a number() field is one. Unlisted field types — time among them — keep the free-form string behaviour:
use Illuminate\Validation\ValidationException;
// 'age' => '3.5' posted to a number() field in the contact form's details group
try {
Forms::validate($form, $request);
} catch (ValidationException $e) {
$e->errors(); // ['contact.details.age' => ['The Age field must be a valid integer.']]
}
$field->attributeType(); // the AttributeType mapped from the field's typeA stored value that no longer converts — data written before a type change, or straight through createSubmission() — reads back exactly as stored instead of breaking the reader.
Adding your own types
Valid AttributeType values are string, integer, float, boolean, array and datetime. The published field_types array replaces the defaults, so keep the entries you still need. number and range are whole numbers — use a decimal or float field for fractions; a time field is unmapped on purpose (validate it with e.g. date_format:H:i):
// config/forms.php — keep the defaults and add your own field types
'field_types' => [
'number' => 'integer',
'checkbox' => 'boolean',
// ...
'rating' => 'integer', // a custom field type, cast and checked as an int
],Show your open-source love
This package is free and MIT-licensed. If it saves you time, a one-off donation or a Patreon membership keeps it maintained, tested and documented.
More ways to support, including cryptoBy donating, you agree to our donation terms.
Want this built into your product?
We integrate our packages into custom Laravel and AI builds. Tell us what you're working on and we'll reply within 48 hours.