Skip to content

Commit 8a5224c

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: Replaced doctrine annotations with attributes Showing how to pass options
2 parents ba0ab72 + 93fd497 commit 8a5224c

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

components/options_resolver.rst

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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
.. index::

validation/custom_constraint.rst

Lines changed: 9 additions & 4 deletions
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
Creating the Validator itself
5252
-----------------------------
@@ -97,6 +97,11 @@ The validator class only has one required method ``validate()``::
9797
// separate multiple types using pipes
9898
// throw new UnexpectedValueException($value, 'string|int');
9999
}
100+
101+
// access your configuration options like this:
102+
if ('strict' === $constraint->mode) {
103+
// ...
104+
}
100105

101106
if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
102107
// the argument must be a string or an object implementing __toString()
@@ -135,7 +140,7 @@ You can use custom validators like the ones provided by Symfony itself:
135140
136141
/**
137142
* @Assert\NotBlank
138-
* @AcmeAssert\ContainsAlphanumeric
143+
* @AcmeAssert\ContainsAlphanumeric(mode="loose")
139144
*/
140145
protected $name;
141146
@@ -155,7 +160,7 @@ You can use custom validators like the ones provided by Symfony itself:
155160
// ...
156161
157162
#[Assert\NotBlank]
158-
#[AcmeAssert\ContainsAlphanumeric]
163+
#[AcmeAssert\ContainsAlphanumeric(options: ['mode' => 'loose'])]
159164
protected $name;
160165
161166
// ...

0 commit comments

Comments
 (0)