You are reading the documentation for the 2.x version. Switch to the current version 3.x.
Form
Object Berlioz\Form\Form is the main object to manipulate forms.
The class inherits Berlioz\Form\Group, so you can add a form to another form!
Options
| Name | Type | Default value | Description |
|---|---|---|---|
| method | string | “post” | HTTP method for form |
| required | boolean | true | Default requirement for all elements |
Methods
Form::handle(ServerRequestInterface $request): void: handle a server request to formForm::isSubmitted(): bool: form is submittedForm::isValid(): bool: all elements of form pass validationForm::getConstraints(): ConstraintInterface[]: all the thrown constraints by the validationForm::getValue(): array: form value (without treatment by transformers) ; if form is submitted, the value will be the user value else the default valueForm::getFinalValue(): array: same asForm::getValue()with treatment by transformers
Example
Creation of a form.
use Berlioz\Form\Form;
use Berlioz\Form\Group;
use Berlioz\Form\Transformer\JsonTransformer;
use Berlioz\Form\Type\Text;
$address = new Group();
$address
->add(
'address',
Text::class,
[
'label' => 'Address',
'attributes' => ['maxlength' => 128],
]
)
->add(
'address_next',
Text::class,
[
'label' => 'Address (next)',
'required' => false,
'attributes' => ['maxlength' => 128],
]
)
->add(
'zip',
Text::class,
[
'label' => 'Zip code',
'attributes' => ['maxlength' => 5],
]
)
->add(
'city',
Text::class,
[
'label' => 'City',
'attributes' => ['maxlength' => 128],
]
);
$form = new Form('form');
$form
->add('input1', Text::class, ['label' => 'My first input'])
->add('input2', Text::class, ['label' => 'My second input']);
Result of form will be:
[
"input1" => "Input 1 value",
"input2" => "Input 2 value"
]