Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/console/helpers/questionhelper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,11 @@
validate the input by using the :method:`Symfony\\Component\\Validator\\Validation::createCallable`
method::

use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 477 in components/console/helpers/questionhelper.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
use Symfony\Component\Validator\Validation;

$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
$validation = Validation::createCallable(new Regex(
$validation = Validation::createCallable(new Assert\Regex(
pattern: '/^[a-zA-Z]+Bundle$/',
message: 'The name of the bundle should be suffixed with \'Bundle\'',
));
Expand Down
18 changes: 8 additions & 10 deletions components/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -685,21 +685,20 @@
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 688 in components/form.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

class DefaultController extends AbstractController
{
public function new(Request $request): Response
{
$form = $this->createFormBuilder()
->add('task', TextType::class, [
'constraints' => new NotBlank(),
'constraints' => new Assert\NotBlank(),
])
->add('dueDate', DateType::class, [
'constraints' => [
new NotBlank(),
new Type(\DateTime::class),
new Assert\NotBlank(),
new Assert\Type(\DateTime::class),
],
])
->getForm();
Expand All @@ -711,17 +710,16 @@

use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 713 in components/form.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

$form = $formFactory->createBuilder()
->add('task', TextType::class, [
'constraints' => new NotBlank(),
'constraints' => new Assert\NotBlank(),
])
->add('dueDate', DateType::class, [
'constraints' => [
new NotBlank(),
new Type(\DateTime::class),
new Assert\NotBlank(),
new Assert\Type(\DateTime::class),
],
])
->getForm();
Expand Down
4 changes: 2 additions & 2 deletions components/options_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,12 @@
method::

use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 392 in components/options_resolver.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
use Symfony\Component\Validator\Validation;

// ...
$resolver->setAllowedValues('transport', Validation::createIsValidCallable(
new Length(min: 10)
new Assert\Length(min: 10)
));

In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues`
Expand Down
7 changes: 3 additions & 4 deletions components/validator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@
The following example shows how to validate that a string is at least 10
characters long::

use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 33 in components/validator.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidator();
$violations = $validator->validate('Bernhard', [
new Length(min: 10),
new NotBlank(),
new Assert\Length(min: 10),
new Assert\NotBlank(),
]);

if (0 !== count($violations)) {
Expand Down
4 changes: 2 additions & 2 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 57 in controller/upload_file.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

class ProductType extends AbstractType
{
Expand All @@ -75,7 +75,7 @@
// unmapped fields can't define their validation using attributes
// in the associated entity, so you can use the PHP constraint classes
'constraints' => [
new File(
new Assert\File(
maxSize: '1024k',
extensions: ['pdf'],
extensionsMessage: 'Please upload a valid PDF document',
Expand Down
33 changes: 17 additions & 16 deletions form/without_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,18 @@

use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('firstName', TextType::class, [
'constraints' => new Length(min: 3),
'constraints' => new Assert\Length(min: 3),
])
->add('lastName', TextType::class, [
'constraints' => [
new NotBlank(),
new Length(min: 3),
new Assert\NotBlank(),
new Assert\Length(min: 3),
],
])
;
Expand All @@ -113,7 +112,9 @@
``Default`` group when creating the form, or set the correct group on
the constraint you are adding::

new NotBlank(['groups' => ['create', 'update']]);
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 115 in form/without_class.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

new Assert\NotBlank(groups: ['create', 'update']);

.. tip::

Expand All @@ -137,9 +138,7 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 141 in form/without_class.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

public function buildForm(FormBuilderInterface $builder, array $options): void
{
Expand All @@ -152,11 +151,11 @@
{
$resolver->setDefaults([
'data_class' => null,
'constraints' => new Collection([
'firstName' => new Length(min: 3),
'constraints' => new Assert\Collection([
'firstName' => new Assert\Length(min: 3),
'lastName' => [
new NotBlank(),
new Length(min: 3),
new Assert\NotBlank(),
new Assert\Length(min: 3),
],
]),
]);
Expand All @@ -165,12 +164,14 @@
This means you can also do this when using the ``createFormBuilder()`` method
in your controller::

use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 167 in form/without_class.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist

$form = $this->createFormBuilder($defaultData, [
'constraints' => [
'firstName' => new Length(['min' => 3]),
'firstName' => new Assert\Length(min: 3),
'lastName' => [
new NotBlank(),
new Length(['min' => 3]),
new Assert\NotBlank(),
new Assert\Length(min: 3),
],
],
])
Expand Down
9 changes: 4 additions & 5 deletions forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@
// src/Entity/Task.php
namespace App\Entity;

use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints as Assert;

Check failure on line 543 in forms.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Task
Expand All @@ -550,12 +549,12 @@

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('task', new NotBlank());
$metadata->addPropertyConstraint('task', new Assert\NotBlank());

$metadata->addPropertyConstraint('dueDate', new NotBlank());
$metadata->addPropertyConstraint('dueDate', new Assert\NotBlank());
$metadata->addPropertyConstraint(
'dueDate',
new Type(\DateTimeInterface::class)
new Assert\Type(\DateTimeInterface::class)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ If your valid choice list is simple, you can pass them in directly via the
{
$metadata->addPropertyConstraint(
'city',
new Assert\Choice(['New York', 'Berlin', 'Tokyo'])
new Assert\Choice(choices: ['New York', 'Berlin', 'Tokyo'])
);

$metadata->addPropertyConstraint('genre', new Assert\Choice(
Expand Down
12 changes: 6 additions & 6 deletions reference/constraints/Collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ following:
'personal_email' => new Assert\Email(),
'short_bio' => [
new Assert\NotBlank(),
new Assert\Length([
'max' => 100,
'maxMessage' => 'Your short bio is too long!',
]),
new Assert\Length(
max: 100,
maxMessage: 'Your short bio is too long!',
),
],
],
allowMissingFields: true,
Expand Down Expand Up @@ -293,8 +293,8 @@ groups. Take the following example::

$constraint = new Assert\Collection(
fields: [
'name' => new Assert\NotBlank(['groups' => 'basic']),
'email' => new Assert\NotBlank(['groups' => 'contact']),
'name' => new Assert\NotBlank(groups: 'basic'),
'email' => new Assert\NotBlank(groups: 'contact'),
],
);

Expand Down
8 changes: 5 additions & 3 deletions validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ following:
// src/Entity/Author.php
namespace App\Entity;
// ...
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class Author
Expand All @@ -104,7 +104,7 @@ following:

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
}
}

Expand Down Expand Up @@ -340,12 +340,14 @@ Constraints in Form Classes
Constraints can be defined while building the form via the ``constraints`` option
of the form fields::

use Symfony\Component\Validator\Constraints as Assert;

public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('myField', TextType::class, [
'required' => true,
'constraints' => [new Length(['min' => 3])],
'constraints' => [new Assert\Length(min: 3)],
])
;
}
Expand Down
8 changes: 4 additions & 4 deletions validation/custom_constraint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ You can use custom validators like the ones provided by Symfony itself:
namespace App\Entity;

use App\Validator\ContainsAlphanumeric;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class User
Expand All @@ -254,7 +254,7 @@ You can use custom validators like the ones provided by Symfony itself:

public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
$metadata->addPropertyConstraint('name', new ContainsAlphanumeric(mode: 'loose'));
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ the custom options like you pass any other option in built-in constraints:
namespace App\Entity;

use App\Validator\ContainsAlphanumeric;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;

class AcmeEntity
Expand All @@ -410,7 +410,7 @@ the custom options like you pass any other option in built-in constraints:

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('name', new NotBlank());
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
$metadata->addPropertyConstraint('name', new Foo(
mandatoryFooOption: 'bar',
optionalBarOption: true,
Expand Down
16 changes: 8 additions & 8 deletions validation/raw_values.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,27 @@ Validation of arrays is possible using the ``Collection`` constraint::
],
];

$groups = new Assert\GroupSequence(['Default', 'custom']);
$groups = new Assert\GroupSequence(groups: ['Default', 'custom']);

$constraint = new Assert\Collection([
// the keys correspond to the keys in the input array
'name' => new Assert\Collection([
'first_name' => new Assert\Length(['min' => 101]),
'last_name' => new Assert\Length(['min' => 1]),
'first_name' => new Assert\Length(min: 101),
'last_name' => new Assert\Length(min: 1),
]),
'email' => new Assert\Email(),
'simple' => new Assert\Length(['min' => 102]),
'eye_color' => new Assert\Choice([3, 4]),
'simple' => new Assert\Length(min: 102),
'eye_color' => new Assert\Choice(choices: [3, 4]),
'file' => new Assert\File(),
'password' => new Assert\Length(['min' => 60]),
'password' => new Assert\Length(min: 60),
'tags' => new Assert\Optional([
new Assert\Type('array'),
new Assert\Count(['min' => 1]),
new Assert\Count(min: 1),
new Assert\All([
new Assert\Collection([
'slug' => [
new Assert\NotBlank(),
new Assert\Type(['type' => 'string']),
new Assert\Type(type: 'string'),
],
'label' => [
new Assert\NotBlank(),
Expand Down
4 changes: 2 additions & 2 deletions validation/translations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ property is not empty, add the following:
namespace App\Entity;
// ...
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class Author
Expand All @@ -83,7 +83,7 @@ property is not empty, add the following:
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('name', new NotBlank(
$metadata->addPropertyConstraint('name', new Assert\NotBlank(
message: 'author.name.not_blank',
));
}
Expand Down
Loading