PostgreSQL rejects malformed text passed to a UUID column with SQLSTATE[22P02]. When a Symfony form accepts an entity identifier from the request, validate the raw value before normal form transformation can trigger an EntityType or Doctrine lookup.
Last updated: November 18, 2026.
<?php
use SymfonyComponentFormFormError;
use SymfonyComponentFormFormEvent;
use SymfonyComponentFormFormEvents;
use SymfonyComponentUidUuid;
$builder->addEventListener(FormEvents::PRE_SUBMIT, static function (FormEvent $event): void {
$data = $event->getData();
$rawId = is_array($data) ? ($data['owner'] ?? null) : null;
if ($rawId === null || $rawId === '') {
return; // Let required/optional form rules handle an empty value.
}
if (!is_string($rawId) || !Uuid::isValid($rawId)) {
$event->getForm()->get('owner')->addError(new FormError('Select a valid owner.'));
$data['owner'] = null;
$event->setData($data);
}
});A parent form’s PRE_SUBMIT listener sees raw request strings before child data transformation. Replacing only a malformed value with null prevents it from becoming a database predicate while preserving normal processing for valid and empty values.
Validate before the database boundary
Symfony’s form event documentation places PRE_SUBMIT before view data is reverse-transformed. It can reject a malformed identifier before an entity choice loader runs. The UID component provides Uuid::isValid(); use its format parameter when accepting only a specific UUID representation.
A longstanding Symfony issue describes invalid EntityType UUID values reaching PostgreSQL in affected configurations. A friendly invalid_message alone may not stop a query during transformation.
Keep empty, invalid and unknown values distinct
An empty optional choice is not the same as malformed input. Let the form’s required, NotBlank and placeholder rules handle it. Reject malformed syntax before lookup. A well-formed UUID that does not identify an allowed entity should produce the normal invalid-choice error.
For an API or route, perform the same Uuid::isValid() check before calling find() or building a query. Test an empty value, arbitrary text, a valid unknown UUID and a valid allowed UUID. Confirm that malformed input returns a client error, not a database exception, and no query contains the rejected value.