@@ -152,6 +152,88 @@ Use this option to define the fully-qualified class name (FQCN) of the Doctrine
152152entity associated with the repository you want to use.
153153Another case is when the validated object is not an entity.
154154
155+
156+ Consider this example:
157+
158+ .. configuration-block ::
159+
160+ .. code-block :: php-annotations
161+
162+ // src/Message/UpdateEmployeeProfile.php
163+ namespace App\Message;
164+
165+ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
166+
167+ /**
168+ * @UniqueEntity(fields={"name": "username"}, entityClass="App\Entity\User", identifierFieldNames={"uid": "id"})
169+ */
170+ class UpdateEmployeeProfile
171+ {
172+ public $uid;
173+ public $name;
174+
175+ public function __construct($uid, $name)
176+ {
177+ $this->uid = $uid;
178+ $this->name = $name;
179+ }
180+ }
181+
182+ .. code-block :: yaml
183+
184+ # config/validator/validation.yaml
185+ App\Message\UpdateEmployeeProfile :
186+ constraints :
187+ - Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity :
188+ fields : [name: username]
189+ entityClass : ' App\Entity\User'
190+ identifierFieldNames : [uid: id]
191+
192+ .. code-block :: xml
193+
194+ <!-- config/validator/validation.xml -->
195+ <?xml version =" 1.0" encoding =" UTF-8" ?>
196+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
197+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
198+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd" >
199+
200+ <class name =" App\Message\UpdateEmployeeProfile" >
201+ <constraint name =" Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity" >
202+ <option name =" fields" >
203+ <value key =" name" >username</value >
204+ </option >
205+ <option name =" entityClass" >App\Entity\User</option >
206+ <option name =" identifierFieldNames" >
207+ <value key =" uid" >id</value >
208+ </option >
209+ </constraint >
210+ </class >
211+
212+ </constraint-mapping >
213+
214+ .. code-block :: php
215+
216+ // src/Message/UpdateEmployeeProfile.php
217+ namespace App\Message;
218+
219+ use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
220+ use Symfony\Component\Validator\Mapping\ClassMetadata;
221+
222+ class UpdateEmployeeProfile
223+ {
224+ public $uid;
225+ public $name;
226+
227+ public static function loadValidatorMetadata(ClassMetadata $metadata)
228+ {
229+ $metadata->addConstraint(new UniqueEntity([
230+ 'fields' => ['name' => 'username'],
231+ 'entityClass' => 'App\Entity\User',
232+ 'identifierFieldNames' => ['uid' => 'id'],
233+ ]));
234+ }
235+ }
236+
155237 errorPath
156238~~~~~~~~~
157239
0 commit comments