civi.afform.validate¶
Summary¶
This Symfony event is dispatched during Afform submission validation. It allows developers to perform custom validation checks and add errors to prevent form submission.
Definition¶
This event is dispatched via the Symfony EventDispatcher.
public static function onAfformValidate(\Civi\Afform\Event\AfformValidateEvent $event): void
Parameters¶
\Civi\Afform\Event\AfformValidateEvent $event
Event Methods¶
getAfform():array- Returns the main Afform configuration array.getFormDataModel():\Civi\Afform\FormDataModel- Returns the form data model instance.getApiRequest():\Civi\Api4\Generic\AbstractAction- Returns the active API request initiating the submit processor.addError(string $errorMsg):void- Appends a validation error message. Adding any error will fail the submission.setErrors(array $errors):void- Replaces all existing validation errors with the specified array of strings.getErrors():array- Returns all validation errors that have been set.getSubmittedValues():array- Returns all submitted entity and field values for the form.getEntityFieldDefn(string $entityName, string $fieldName, ?string $joinEntity = null):array- Returns the schema field definition for a given form entity and field name, merging base configuration and form markup overrides.
Example¶
use Civi\Afform\Event\AfformValidateEvent;
public static function onAfformValidate(AfformValidateEvent $event): void {
$values = $event->getSubmittedValues();
// Custom validation rule: ensure email field contains a specific domain
$email = $values['Individual1']['email'][0]['email'] ?? '';
if ($email && !str_ends_with($email, '@example.com')) {
$event->addError('Email address must end with @example.com');
}
}