Create a form
Table of content:
Get a form
Forms are created by the \Ameos\ (inject it in your controller):
use Ameos\AmeosForm\Service\FormService;
public function __construct(private readonly FormService $formService)
{
}
public function editAction(?MyModel $mymodel = null): ResponseInterface
{
$form = $this->formService->create('tx_myextension_myplugin', $mymodel ?? new MyModel(), $this->request);
...
}
create
$identifier: name of the form, fields are namedidentifier. Use the plugin namespace ([field] tx_) to get the error flash messages in the queue of the plugin.myextension_ myplugin $subject: an Extbase entity (edition form), a searchable repository (search form) or nothing.$request: the current request ($this->requestin an Extbase controller). Defaults to$GLOBALS.['TYPO3_ REQUEST']
Important
The form reads the submitted values, stores the CSRF token, the captcha digits and the search in the frontend user session: the actions displaying a form must be non-cacheable.
Creation and edition form
When an entity is given, each element is initialized with the value of its getter (get for
the element my_), and each submitted value is given to its setter (set).
Submitted values are strings: they are cast to the type of the setter parameter (int, float,
bool, string; arrays are converted to comma separated lists for string), and an empty string
becomes null when the parameter is nullable. Dates are given as \Date (or \Date
when the setter expects it).
Unchecked checkboxes and multiple select boxes without selection are not sent by the browser: when the form is submitted, these elements are emptied (except when they are disabled).
public function editAction(?MyModel $mymodel = null): ResponseInterface
{
$mymodel ??= new MyModel();
$form = $this->formService->create('tx_myplugin_pi1', $mymodel, $this->request);
$form->add('name', 'text')->add('email', 'email')->add('submit', 'submit', ['label' => 'Send']);
if ($form->isSubmitted()) {
$form->addConstraint('name', 'required', 'Name is mandatory')
->addConstraint('email', 'email', 'Email is not valid');
if ($form->isValid()) {
$mymodel->getUid() === null ? $this->myModelRepository->add($mymodel) : $this->myModelRepository->update($mymodel);
$this->addFlashMessage('Record saved');
return $this->redirect('list');
}
}
$this->view->assign('form', $form);
return $this->htmlResponse();
}
The entity is modified as soon as the elements are added: persist it only when the form is valid.
Search form
Give a repository implementing \Ameos\.
The easiest way is to extend \Ameos\ (or to use the trait
\Ameos\ in an Extbase repository).
$searchform = $this->formService->create('tx_myextension_search', $this->myRepository, $this->request);
$searchform->add('title', 'text')->add('submit', 'submit', ['label' => 'Search']);
$this->view->assign('searchform', $searchform);
$this->view->assign('results', $searchform->getResults('title', 'ASC'));
Each element adds a clause (like for text fields, equals for dropdown, radio and range, in for
multiple dropdown, contains for checkbox). The clause of an element can be replaced:
$searchform->get('year_min')->setOverrideClause(
fn(mixed $value): array => ['field' => 'year', 'type' => 'greaterThanOrEqual', 'value' => (int)$value]
);
You can add where clauses with "addWhereClause" method.
$searchform->addWhereClause([
'type' => 'logicalOr',
'clauses' => [
['field' => 'myfield', 'type' => 'like', 'value' => 'myvalue1'],
['field' => 'myfield', 'type' => 'like', 'value' => 'myvalue2'],
]
]);
$searchform->addWhereClause(['field' => 'myfield', 'type' => 'equals', 'value' => 'myvalue']);
Available clause types: equals, like, contains (relations), in (comma separated values
stored in a string field), in, less, less, greater, greater,
logical / logical (with clauses) and logical (with clause).
The search is stored in the frontend user session and restored when the form is displayed again without
being submitted. Disable it with $searchform->disable.
Other form
You can create a form without model or repository link.
For example, a contact form without database insertion.
$form = $this->formService->create('tx_myextension_contact', null, $this->request);
Read the values with $form->get once the form is valid.
Add an element to the form
After the creation of your form, you add element easily.
$form->add($name, $type, $configuration);
For example : $form->add
The types are available as constants of \Ameos\ (Element::...).
All elements are detailled on Elements part.
Add a constraint to the form
After the creation of your form, you add constraint easily.
$form->addConstraint($field, $type, $message, $configuration); // $configuration is optional. Depends the type of validator
For example : $form->add
The types are available as constants of \Ameos\.
All constraints are detailled on Constraints part.
Errors are added as flash messages in the queue extbase.. Disable them
with $form->get.
When the form is valid, the \Ameos\ PSR-14 event is dispatched (once): it
hashes the passwords and moves the uploaded files. Submitted values can be changed by listening to
\Ameos\.
Form protection
The form ViewHelper adds a CSRF token (stored in the frontend user session) and a honeypot field (hidden
field which must stay empty). Both are checked when the form is submitted: when they are not valid, an error
is added with the key \Ameos\ and the form is not valid.
If you want, you can disable form protection.
Csrf protection :
$form->disableCsrftoken();
Honeypot protection :
$form->disableHoneypot();