validateFormField
The validateFormField
hook is triggered when a form field is submitted. It
passes the widget object and the form ID as arguments and expects a widget
object as return value.
Parameters
\Contao\Widget
$widget
Object of the current front end widget. Use it to access form field properties.
string
$formId
Alias of the current form. Used in the
value
attribute of the hidden form fieldFORM_SUBMIT
. Do not confuse with$form->id
.array
$formData
Form configuration data from the
tl_form
table.\Contao\Form
$form
The form instance.
Return Value
Return the $widget
instance after modification or your custom widget.
Example
// src/EventListener/ValidateFormFieldListener.php
namespace App\EventListener;
use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Form;
use Contao\Widget;
#[AsHook('validateFormField')]
class ValidateFormFieldListener
{
public function __invoke(Widget $widget, string $formId, array $formData, Form $form): Widget
{
if ('myform' === $formId && 'mywidget' === $widget->name) {
// Do your custom validation and add an error if widget does not validate
// …
$widget->addError('My custom widget error');
}
return $widget;
}
}