Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdcbad67

Browse files
committed
Added basic example
1 parentcaf3e1f commitdcbad67

File tree

1 file changed

+83
-3
lines changed

1 file changed

+83
-3
lines changed

‎reference/constraints/Unique.rst

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,89 @@ Options
113113

114114
The ``fields`` option was introduced in Symfony 6.1.
115115

116-
This is optional and defines key or keys
117-
in a collection that should be checked for uniqueness.
118-
By default, all collection keys are checked for uniqueness.
116+
This is defines the key or keys in a collection that should be checked for
117+
uniqueness. By default, all collection keys are checked for uniqueness.
118+
119+
For instance, assume you have a collection of items that contain a
120+
``latitude``, ``longitude`` and ``label`` fields. By default, you can have
121+
duplicate coordinates as long as the label is different. By setting the
122+
``fields`` option, you can force latitude+longitude to be unique in the
123+
collection::
124+
125+
..configuration-block::
126+
127+
..code-block::php-annotations
128+
129+
// src/Entity/Poi.php
130+
namespace App\Entity;
131+
132+
use Symfony\Component\Validator\Constraints as Assert;
133+
134+
class Poi
135+
{
136+
/**
137+
* @Assert\Unique(fields={"latitude", "longitude"})
138+
*/
139+
protected $coordinates;
140+
}
141+
142+
..code-block::php-attributes
143+
144+
// src/Entity/Poi.php
145+
namespace App\Entity;
146+
147+
use Symfony\Component\Validator\Constraints as Assert;
148+
149+
class Poi
150+
{
151+
#[Assert\Unique(fields=['latitude', 'longitude'])]
152+
protected $coordinates;
153+
}
154+
155+
..code-block::yaml
156+
157+
# config/validator/validation.yaml
158+
App\Entity\Poi:
159+
properties:
160+
contactEmails:
161+
-Unique:
162+
fields:[latitude, longitude]
163+
164+
..code-block::xml
165+
166+
<!-- config/validator/validation.xml-->
167+
<?xml version="1.0" encoding="UTF-8" ?>
168+
<constraint-mappingxmlns="http://symfony.com/schema/dic/constraint-mapping"
169+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
170+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
171+
172+
<classname="App\Entity\Poi">
173+
<propertyname="coordinates">
174+
<constraintname="Unique">
175+
<field>latitude</field>
176+
<field>longitude</field>
177+
</constraint>
178+
</property>
179+
</class>
180+
</constraint-mapping>
181+
182+
..code-block::php
183+
184+
// src/Entity/Poi.php
185+
namespace App\Entity;
186+
187+
use Symfony\Component\Validator\Constraints as Assert;
188+
use Symfony\Component\Validator\Mapping\ClassMetadata;
189+
190+
class Poi
191+
{
192+
public static function loadValidatorMetadata(ClassMetadata $metadata)
193+
{
194+
$metadata->addPropertyConstraint('contactEmails', new Assert\Unique([
195+
'fields' => ['latitude', 'longitude'],
196+
]));
197+
}
198+
}
119199
120200
..include::/reference/constraints/_groups-option.rst.inc
121201

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp