Field types¶
Every Small Forms field has an AbstractType. A type can contribute validation rules and modifiers automatically when the field is created.
All eight built-in types are declared as PHP attributes, so they can be used both programmatically and directly on entity properties.
| Type | Constructor | Built-in behavior |
|---|---|---|
StringType |
new StringType() |
Adds ValidateString. |
IntType |
new IntType() |
Adds ValidateInt. |
FloatType |
new FloatType() |
Adds ValidateFloat. |
BooleanType |
new BooleanType($falseValue = false, $trueValue = true) |
Adds boolean validation and false/true normalization modifiers. |
DateTimeType |
new DateTimeType($format = 'Y-m-d H:i:s') |
Adds date validation and conversion to mutable DateTime. |
DateTimeImmutableType |
new DateTimeImmutableType($format = 'Y-m-d H:i:s') |
Adds date validation and conversion to DateTimeImmutable. |
ArrayType |
new ArrayType(AbstractType $type) |
Describes the type of every nested item. |
SubFormType |
new SubFormType(string $fromClass) |
Describes a nested object/form. |
Scalar fields¶
#[StringType]
#[ValidateNotEmpty]
private string $name = '';
#[IntType]
#[ValidateGreaterOrEqual(1)]
private int $quantity = 1;
#[FloatType]
private float $price = 0.0;
A type's own rules are appended to rules supplied explicitly to the field.
Boolean input values¶
BooleanType supports transport-specific true and false values:
The type adds, in order:
ValidateBoolean;FalseIfEmptyModifier;FormBooleanToPhpModifier.
The stored form value is not rewritten by these modifiers. They are applied when getFieldValue() or hydrate() reads the value.
Date and time¶
#[DateTimeType('Y-m-d H:i:s')]
private ?\DateTime $createdAt = null;
#[DateTimeImmutableType('Y-m-d')]
private ?\DateTimeImmutable $publicationDate = null;
Both date types add ValidateDateTime plus the corresponding conversion modifier.
Input vs database formats
The Small Forms date format is an input/output contract. Entity Manager's Field format controls database conversion. They can be the same, but they are independent settings.
Arrays¶
An array type always contains another Small Forms type:
Nested arrays are supported:
For input validation, the nested type validates each item. Add array-level rules such as ValidateCountGreaterThan, ValidateArrayKeys or ValidateArrayShape separately when needed.
Nested objects¶
SubFormType verifies that the target class exists at construction time. Its value is represented internally as a nested AbstractForm when input is supplied as an array/object.
Arrays of nested objects¶
Each input item becomes its own nested form. During hydration, Small Forms recursively creates and hydrates destination objects according to the destination property type and available factory methods.
Entity Manager relation factories add additional behavior for AbstractEntity; see Relations and nested forms.