8000 Merge branch '4.3' into 4.4 · symfony/symfony-docs@752c1f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 752c1f2

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: Highlighting possibilty to use FQCN as type value.
2 parents 2cfda03 + b357c02 commit 752c1f2

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

reference/constraints/Type.rst

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ Validator :class:`Symfony\\Component\\Validator\\Constraints\\TypeValidator`
1818
Basic Usage
1919
-----------
2020

21-
This will check if ``firstName`` is of type ``string`` (using :phpfunction:`is_string`
22-
PHP function), ``age`` is an ``integer`` (using :phpfunction:`is_int` PHP
23-
function) and ``accessCode`` contains either only letters or only digits (using
21+
This will check if ``id`` is an instance of ``Ramsey\Uuid\UuidInterface``,
22+
``firstName`` is of type ``string`` (using :phpfunction:`is_string` PHP function),
23+
``age`` is an ``integer`` (using :phpfunction:`is_int` PHP function) and
24+
``accessCode`` contains either only letters or only digits (using
2425
:phpfunction:`ctype_alpha` and :phpfunction:`ctype_digit` PHP functions).
2526

2627
.. configuration-block::
@@ -34,6 +35,11 @@ function) and ``accessCode`` contains either only letters or only digits (using
3435
3536
class Author
3637
{
38+
/**
39+
* @Assert\Type("Ramsey\Uuid\UuidInterface")
40+
*/
41+
protected $id;
42+
3743
/**
3844
* @Assert\Type("string")
3945
*/
@@ -58,6 +64,9 @@ function) and ``accessCode`` contains either only letters or only digits (using
5864
# config/validator/validation.yaml
5965
App\Entity\Author:
6066
properties:
67+
id:
68+
- Type: Ramsey\Uuid\UuidInterface
69+
6170
firstName:
6271
- Type: string
6372
@@ -79,6 +88,11 @@ function) and ``accessCode`` contains either only letters or only digits (using
7988
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
8089
8190
<class name="App\Entity\Author">
91+
<property name="id">
92+
<constraint name="Type">
93+
<option name="type">Ramsey\Uuid\UuidInterface</option>
94+
</constraint>
95+
</property>
8296
<property name="firstName">
8397
<constraint name="Type">
8498
<option name="type">string</option>
@@ -106,13 +120,16 @@ function) and ``accessCode`` contains either only letters or only digits (using
106120
// src/Entity/Author.php
107121
namespace App\Entity;
108122
123+
use Ramsey\Uuid\UuidInterface;
109124
use Symfony\Component\Validator\Constraints as Assert;
110125
use Symfony\Component\Validator\Mapping\ClassMetadata;
111126
112127
class Author
113128
{
114129
public static function loadValidatorMetadata(ClassMetadata $metadata)
115130
{
131+
$metadata->addPropertyConstraint('id', new Assert\Type(UuidInterface::class));
132+
116133
$metadata->addPropertyConstraint('firstName', new Assert\Type('string'));
117134
118135
$metadata->addPropertyConstraint('age', new Assert\Type([
@@ -167,8 +184,8 @@ type
167184
in Symfony 4.4.
168185

169186
This required option defines the type or collection of types allowed for the
170-
given value. Each type is defined as the fully qualified class name or one of
171-
the PHP datatypes as determined by PHP's ``is_*()`` functions.
187+
given value. Each type is is either the FQCN (fully qualified class name) of some
188+
PHP class/interface or a valid PHP datatype (checked by PHP's ``is_()`` functions):
172189

173190
* :phpfunction:`array <is_array>`
174191
* :phpfunction:`bool <is_bool>`

0 commit comments

Comments
 (0)
0