Skip to content

Commit 956ed61

Browse files
authored
Update UniqueEntity.rst
1 parent 3bc2ab0 commit 956ed61

File tree

1 file changed

+81
-82
lines changed

1 file changed

+81
-82
lines changed

reference/constraints/UniqueEntity.rst

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -152,88 +152,6 @@ Use this option to define the fully-qualified class name (FQCN) of the Doctrine
152152
entity associated with the repository you want to use.
153153
Another 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-
237155
errorPath
238156
~~~~~~~~~
239157

@@ -366,6 +284,87 @@ If validated object field names do not match the ones from the entity,
366284
use key-value mapping; Where ``key`` is the name of the field in the validated object
367285
and ``value`` is the name of the field in the entity.
368286

287+
Consider this example:
288+
289+
.. configuration-block::
290+
291+
.. code-block:: php-annotations
292+
293+
// src/Message/UpdateEmployeeProfile.php
294+
namespace App\Message;
295+
296+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
297+
298+
/**
299+
* @UniqueEntity(fields={"name": "username"}, entityClass="App\Entity\User", identifierFieldNames={"uid": "id"})
300+
*/
301+
class UpdateEmployeeProfile
302+
{
303+
public $uid;
304+
public $name;
305+
306+
public function __construct($uid, $name)
307+
{
308+
$this->uid = $uid;
309+
$this->name = $name;
310+
}
311+
}
312+
313+
.. code-block:: yaml
314+
315+
# config/validator/validation.yaml
316+
App\Message\UpdateEmployeeProfile:
317+
constraints:
318+
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
319+
fields: {name: username}
320+
entityClass: 'App\Entity\User'
321+
identifierFieldNames: {uid: id}
322+
323+
.. code-block:: xml
324+
325+
<!-- config/validator/validation.xml -->
326+
<?xml version="1.0" encoding="UTF-8" ?>
327+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
328+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
329+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
330+
331+
<class name="App\Message\UpdateEmployeeProfile">
332+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
333+
<option name="fields">
334+
<value key="name" >username</value>
335+
</option>
336+
<option name="entityClass">App\Entity\User</option>
337+
<option name="identifierFieldNames">
338+
<value key="uid" >id</value>
339+
</option>
340+
</constraint>
341+
</class>
342+
343+
</constraint-mapping>
344+
345+
.. code-block:: php
346+
347+
// src/Message/UpdateEmployeeProfile.php
348+
namespace App\Message;
349+
350+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
351+
use Symfony\Component\Validator\Mapping\ClassMetadata;
352+
353+
class UpdateEmployeeProfile
354+
{
355+
public $uid;
356+
public $name;
357+
358+
public static function loadValidatorMetadata(ClassMetadata $metadata)
359+
{
360+
$metadata->addConstraint(new UniqueEntity([
361+
'fields' => ['name' => 'username'],
362+
'entityClass' => 'App\Entity\User',
363+
'identifierFieldNames' => ['uid' => 'id'],
364+
]));
365+
}
366+
}
367+
369368
.. versionadded:: 5.2
370369

371370
The option was introduced in Symfony 5.2.

0 commit comments

Comments
 (0)