Skip to content

Commit 93fd497

Browse files
committed
Merge branch '5.3' into 5.4
* 5.3: Replaced doctrine annotations with attributes Showing how to pass options
2 parents 2ccf9c9 + 4b01042 commit 93fd497

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

components/options_resolver.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,15 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
381381
.. tip::
382382

383383
You can even use the :doc:`Validator </validation>` component to validate the
384-
input by using the :method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
384+
input by using the :method:`Symfony\\Component\\Validator\\Validation::createIsValidCallable`
385385
method::
386386

387387
use Symfony\Component\OptionsResolver\OptionsResolver;
388388
use Symfony\Component\Validator\Constraints\Length;
389389
use Symfony\Component\Validator\Validation;
390390

391391
// ...
392-
$resolver->setAllowedValues('transport', Validation::createValidCallable(
392+
$resolver->setAllowedValues('transport', Validation::createIsValidCallable(
393393
new Length(['min' => 10 ])
394394
));
395395

reference/constraints/Traverse.rst

+5-8
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,27 @@ that all have constraints on their properties.
9494
// src/Entity/BookCollection.php
9595
namespace App\Entity;
9696
97+
use App\Entity\Book;
9798
use Doctrine\Common\Collections\ArrayCollection;
98-
use Doctrine\Common\Collections\Collection
99+
use Doctrine\Common\Collections\Collection;
99100
use Doctrine\ORM\Mapping as ORM;
100101
use Symfony\Component\Validator\Constraints as Assert;
101102
102-
/**
103-
* @ORM\Entity
104-
*/
103+
#[ORM\Entity]
105104
#[Assert\Traverse]
106105
class BookCollection implements \IteratorAggregate
107106
{
108107
/**
109108
* @var string
110-
*
111-
* @ORM\Column
112109
*/
110+
#[ORM\Column]
113111
#[Assert\NotBlank]
114112
protected $name = '';
115113
116114
/**
117115
* @var Collection|Book[]
118-
*
119-
* @ORM\ManyToMany(targetEntity="App\Entity\Book")
120116
*/
117+
#[ORM\ManyToMany(targetEntity: Book::class)]
121118
protected $books;
122119
123120
// some other properties

validation.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ when :ref:`validating OptionsResolver values <optionsresolver-validate-value>`):
244244
:method:`Symfony\\Component\\Validator\\Validation::createCallable`
245245
This returns a closure that throws ``ValidationFailedException`` when the
246246
constraints aren't matched.
247-
:method:`Symfony\\Component\\Validator\\Validation::createValidCallable`
247+
:method:`Symfony\\Component\\Validator\\Validation::createIsValidCallable`
248248
This returns a closure that returns ``false`` when the constraints aren't matched.
249249

250250
.. versionadded:: 5.1
@@ -253,7 +253,7 @@ when :ref:`validating OptionsResolver values <optionsresolver-validate-value>`):
253253

254254
.. versionadded:: 5.3
255255

256-
``Validation::createValidCallable()`` was introduced in Symfony 5.3.
256+
``Validation::createIsValidCallable()`` was introduced in Symfony 5.3.
257257

258258
.. index::
259259
single: Validation; Constraints

validation/custom_constraint.rst

+9-4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
2929
class ContainsAlphanumeric extends Constraint
3030
{
3131
public $message = 'The string "{{ string }}" contains an illegal character: it can only contain letters or numbers.';
32+
public $mode = 'strict'; // If the constraint has configuration options, define them as public properties
3233
}
3334
3435
.. code-block:: php-attributes
@@ -45,8 +46,7 @@ First you need to create a Constraint class and extend :class:`Symfony\\Componen
4546
}
4647
4748
Add ``@Annotation`` or ``#[\Attribute]`` to the constraint class if you want to
48-
use it as an annotation/attribute in other classes. If the constraint has
49-
configuration options, define them as public properties on the constraint class.
49+
use it as an annotation/attribute in other classes.
5050

5151
.. versionadded:: 5.2
5252

@@ -103,6 +103,11 @@ The validator class only has one required method ``validate()``::
103103
// separate multiple types using pipes
104104
// throw new UnexpectedValueException($value, 'string|int');
105105
}
106+
107+
// access your configuration options like this:
108+
if ('strict' === $constraint->mode) {
109+
// ...
110+
}
106111

107112
if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
108113
// the argument must be a string or an object implementing __toString()
@@ -141,7 +146,7 @@ You can use custom validators like the ones provided by Symfony itself:
141146
142147
/**
143148
* @Assert\NotBlank
144-
* @AcmeAssert\ContainsAlphanumeric
149+
* @AcmeAssert\ContainsAlphanumeric(mode="loose")
145150
*/
146151
protected $name;
147152
@@ -161,7 +166,7 @@ You can use custom validators like the ones provided by Symfony itself:
161166
// ...
162167
163168
#[Assert\NotBlank]
164-
#[AcmeAssert\ContainsAlphanumeric]
169+
#[AcmeAssert\ContainsAlphanumeric(options: ['mode' => 'loose'])]
165170
protected $name;
166171
167172
// ...

0 commit comments

Comments
 (0)