Skip to content

Commit 4b4161f

Browse files
committed
Consistently use Assert\... when referring to assertions
1 parent f7c9a76 commit 4b4161f

File tree

13 files changed

+64
-65
lines changed

13 files changed

+64
-65
lines changed

components/console/helpers/questionhelper.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,11 @@ invalid answer and will only be able to proceed if their input is valid.
474474
validate the input by using the :method:`Symfony\\Component\\Validator\\Validation::createCallable`
475475
method::
476476

477-
use Symfony\Component\Validator\Constraints\Regex;
477+
use Symfony\Component\Validator\Constraints as Assert;
478478
use Symfony\Component\Validator\Validation;
479479

480480
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
481-
$validation = Validation::createCallable(new Regex(
481+
$validation = Validation::createCallable(new Assert\Regex(
482482
pattern: '/^[a-zA-Z]+Bundle$/',
483483
message: 'The name of the bundle should be suffixed with \'Bundle\'',
484484
));

components/form.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -685,21 +685,20 @@ option when building each field:
685685
use Symfony\Component\Form\Extension\Core\Type\DateType;
686686
use Symfony\Component\Form\Extension\Core\Type\TextType;
687687
use Symfony\Component\HttpFoundation\Response;
688-
use Symfony\Component\Validator\Constraints\NotBlank;
689-
use Symfony\Component\Validator\Constraints\Type;
688+
use Symfony\Component\Validator\Constraints as Assert;
690689
691690
class DefaultController extends AbstractController
692691
{
693692
public function new(Request $request): Response
694693
{
695694
$form = $this->createFormBuilder()
696695
->add('task', TextType::class, [
697-
'constraints' => new NotBlank(),
696+
'constraints' => new Assert\NotBlank(),
698697
])
699698
->add('dueDate', DateType::class, [
700699
'constraints' => [
701-
new NotBlank(),
702-
new Type(\DateTime::class),
700+
new Assert\NotBlank(),
701+
new Assert\Type(\DateTime::class),
703702
],
704703
])
705704
->getForm();
@@ -711,17 +710,16 @@ option when building each field:
711710
712711
use Symfony\Component\Form\Extension\Core\Type\DateType;
713712
use Symfony\Component\Form\Extension\Core\Type\TextType;
714-
use Symfony\Component\Validator\Constraints\NotBlank;
715-
use Symfony\Component\Validator\Constraints\Type;
713+
use Symfony\Component\Validator\Constraints as Assert;
716714
717715
$form = $formFactory->createBuilder()
718716
->add('task', TextType::class, [
719-
'constraints' => new NotBlank(),
717+
'constraints' => new Assert\NotBlank(),
720718
])
721719
->add('dueDate', DateType::class, [
722720
'constraints' => [
723-
new NotBlank(),
724-
new Type(\DateTime::class),
721+
new Assert\NotBlank(),
722+
new Assert\Type(\DateTime::class),
725723
],
726724
])
727725
->getForm();

components/options_resolver.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,12 @@ returns ``true`` for acceptable values and ``false`` for invalid values::
389389
method::
390390

391391
use Symfony\Component\OptionsResolver\OptionsResolver;
392-
use Symfony\Component\Validator\Constraints\Length;
392+
use Symfony\Component\Validator\Constraints as Assert;
393393
use Symfony\Component\Validator\Validation;
394394

395395
// ...
396396
$resolver->setAllowedValues('transport', Validation::createIsValidCallable(
397-
new Length(min: 10)
397+
new Assert\Length(min: 10)
398398
));
399399

400400
In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`

components/validator.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ The Validator component behavior is based on two concepts:
3030
The following example shows how to validate that a string is at least 10
3131
characters long::
3232

33-
use Symfony\Component\Validator\Constraints\Length;
34-
use Symfony\Component\Validator\Constraints\NotBlank;
33+
use Symfony\Component\Validator\Constraints as Assert;
3534
use Symfony\Component\Validator\Validation;
3635

3736
$validator = Validation::createValidator();
3837
$violations = $validator->validate('Bernhard', [
39-
new Length(min: 10),
40-
new NotBlank(),
38+
new Assert\Length(min: 10),
39+
new Assert\NotBlank(),
4140
]);
4241

4342
if (0 !== count($violations)) {

controller/upload_file.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ so Symfony doesn't try to get/set its value from the related entity::
5454
use Symfony\Component\Form\Extension\Core\Type\FileType;
5555
use Symfony\Component\Form\FormBuilderInterface;
5656
use Symfony\Component\OptionsResolver\OptionsResolver;
57-
use Symfony\Component\Validator\Constraints\File;
57+
use Symfony\Component\Validator\Constraints as Assert;
5858

5959
class ProductType extends AbstractType
6060
{
@@ -75,7 +75,7 @@ so Symfony doesn't try to get/set its value from the related entity::
7575
// unmapped fields can't define their validation using attributes
7676
// in the associated entity, so you can use the PHP constraint classes
7777
'constraints' => [
78-
new File(
78+
new Assert\File(
7979
maxSize: '1024k',
8080
extensions: ['pdf'],
8181
extensionsMessage: 'Please upload a valid PDF document',

form/without_class.rst

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,18 @@ but here's a short example::
8989

9090
use Symfony\Component\Form\Extension\Core\Type\TextType;
9191
use Symfony\Component\Form\FormBuilderInterface;
92-
use Symfony\Component\Validator\Constraints\Length;
93-
use Symfony\Component\Validator\Constraints\NotBlank;
92+
use Symfony\Component\Validator\Constraints as Assert;
9493

9594
public function buildForm(FormBuilderInterface $builder, array $options): void
9695
{
9796
$builder
9897
->add('firstName', TextType::class, [
99-
'constraints' => new Length(min: 3),
98+
'constraints' => new Assert\Length(min: 3),
10099
])
101100
->add('lastName', TextType::class, [
102101
'constraints' => [
103-
new NotBlank(),
104-
new Length(min: 3),
102+
new Assert\NotBlank(),
103+
new Assert\Length(min: 3),
105104
],
106105
])
107106
;
@@ -113,7 +112,9 @@ but here's a short example::
113112
``Default`` group when creating the form, or set the correct group on
114113
the constraint you are adding::
115114

116-
new NotBlank(['groups' => ['create', 'update']]);
115+
use Symfony\Component\Validator\Constraints as Assert;
116+
117+
new Assert\NotBlank(groups: ['create', 'update']);
117118

118119
.. tip::
119120

@@ -137,9 +138,7 @@ This can be done by setting the ``constraints`` option in the
137138
use Symfony\Component\Form\Extension\Core\Type\TextType;
138139
use Symfony\Component\Form\FormBuilderInterface;
139140
use Symfony\Component\OptionsResolver\OptionsResolver;
140-
use Symfony\Component\Validator\Constraints\Collection;
141-
use Symfony\Component\Validator\Constraints\Length;
142-
use Symfony\Component\Validator\Constraints\NotBlank;
141+
use Symfony\Component\Validator\Constraints as Assert;
143142

144143
public function buildForm(FormBuilderInterface $builder, array $options): void
145144
{
@@ -152,11 +151,11 @@ This can be done by setting the ``constraints`` option in the
152151
{
153152
$resolver->setDefaults([
154153
'data_class' => null,
155-
'constraints' => new Collection([
156-
'firstName' => new Length(min: 3),
154+
'constraints' => new Assert\Collection([
155+
'firstName' => new Assert\Length(min: 3),
157156
'lastName' => [
158-
new NotBlank(),
159-
new Length(min: 3),
157+
new Assert\NotBlank(),
158+
new Assert\Length(min: 3),
160159
],
161160
]),
162161
]);
@@ -165,12 +164,14 @@ This can be done by setting the ``constraints`` option in the
165164
This means you can also do this when using the ``createFormBuilder()`` method
166165
in your controller::
167166

167+
use Symfony\Component\Validator\Constraints as Assert;
168+
168169
$form = $this->createFormBuilder($defaultData, [
169170
'constraints' => [
170-
'firstName' => new Length(['min' => 3]),
171+
'firstName' => new Assert\Length(min: 3),
171172
'lastName' => [
172-
new NotBlank(),
173-
new Length(['min' => 3]),
173+
new Assert\NotBlank(),
174+
new Assert\Length(min: 3),
174175
],
175176
],
176177
])

forms.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,8 +540,7 @@ object.
540540
// src/Entity/Task.php
541541
namespace App\Entity;
542542
543-
use Symfony\Component\Validator\Constraints\NotBlank;
544-
use Symfony\Component\Validator\Constraints\Type;
543+
use Symfony\Component\Validator\Constraints as Assert;
545544
use Symfony\Component\Validator\Mapping\ClassMetadata;
546545
547546
class Task
@@ -550,12 +549,12 @@ object.
550549
551550
public static function loadValidatorMetadata(ClassMetadata $metadata): void
552551
{
553-
$metadata->addPropertyConstraint('task', new NotBlank());
552+
$metadata->addPropertyConstraint('task', new Assert\NotBlank());
554553
555-
$metadata->addPropertyConstraint('dueDate', new NotBlank());
554+
$metadata->addPropertyConstraint('dueDate', new Assert\NotBlank());
556555
$metadata->addPropertyConstraint(
557556
'dueDate',
558-
new Type(\DateTimeInterface::class)
557+
new Assert\Type(\DateTimeInterface::class)
559558
);
560559
}
561560
}

reference/constraints/Choice.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ If your valid choice list is simple, you can pass them in directly via the
9797
{
9898
$metadata->addPropertyConstraint(
9999
'city',
100-
new Assert\Choice(['New York', 'Berlin', 'Tokyo'])
100+
new Assert\Choice(choices: ['New York', 'Berlin', 'Tokyo'])
101101
);
102102
103103
$metadata->addPropertyConstraint('genre', new Assert\Choice(

reference/constraints/Collection.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ following:
144144
'personal_email' => new Assert\Email(),
145145
'short_bio' => [
146146
new Assert\NotBlank(),
147-
new Assert\Length([
148-
'max' => 100,
149-
'maxMessage' => 'Your short bio is too long!',
150-
]),
147+
new Assert\Length(
148+
max: 100,
149+
maxMessage: 'Your short bio is too long!',
150+
),
151151
],
152152
],
153153
allowMissingFields: true,
@@ -293,8 +293,8 @@ groups. Take the following example::
293293

294294
$constraint = new Assert\Collection(
295295
fields: [
296-
'name' => new Assert\NotBlank(['groups' => 'basic']),
297-
'email' => new Assert\NotBlank(['groups' => 'contact']),
296+
'name' => new Assert\NotBlank(groups: 'basic'),
297+
'email' => new Assert\NotBlank(groups: 'contact'),
298298
],
299299
);
300300

validation.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ following:
9595
// src/Entity/Author.php
9696
namespace App\Entity;
9797
// ...
98-
use Symfony\Component\Validator\Constraints\NotBlank;
98+
use Symfony\Component\Validator\Constraints as Assert;
9999
use Symfony\Component\Validator\Mapping\ClassMetadata;
100100
101101
class Author
@@ -104,7 +104,7 @@ following:
104104
105105
public static function loadValidatorMetadata(ClassMetadata $metadata): void
106106
{
107-
$metadata->addPropertyConstraint('name', new NotBlank());
107+
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
108108
}
109109
}
110110
@@ -340,12 +340,14 @@ Constraints in Form Classes
340340
Constraints can be defined while building the form via the ``constraints`` option
341341
of the form fields::
342342

343+
use Symfony\Component\Validator\Constraints as Assert;
344+
343345
public function buildForm(FormBuilderInterface $builder, array $options): void
344346
{
345347
$builder
346348
->add('myField', TextType::class, [
347349
'required' => true,
348-
'constraints' => [new Length(['min' => 3])],
350+
'constraints' => [new Assert\Length(min: 3)],
349351
])
350352
;
351353
}

0 commit comments

Comments
 (0)