diff --git a/components/console/helpers/questionhelper.rst b/components/console/helpers/questionhelper.rst index 3dc97d5c0d3..c7e064b16ca 100644 --- a/components/console/helpers/questionhelper.rst +++ b/components/console/helpers/questionhelper.rst @@ -480,10 +480,10 @@ invalid answer and will only be able to proceed if their input is valid. use Symfony\Component\Validator\Validation; $question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle'); - $validation = Validation::createCallable(new Regex([ - 'pattern' => '/^[a-zA-Z]+Bundle$/', - 'message' => 'The name of the bundle should be suffixed with \'Bundle\'', - ])); + $validation = Validation::createCallable(new Regex( + pattern: '/^[a-zA-Z]+Bundle$/', + message: 'The name of the bundle should be suffixed with \'Bundle\'', + )); $question->setValidator($validation); Validating a Hidden Response diff --git a/components/options_resolver.rst b/components/options_resolver.rst index 95741a7e372..65685106c13 100644 --- a/components/options_resolver.rst +++ b/components/options_resolver.rst @@ -394,7 +394,7 @@ returns ``true`` for acceptable values and ``false`` for invalid values:: // ... $resolver->setAllowedValues('transport', Validation::createIsValidCallable( - new Length(['min' => 10 ]) + new Length(min: 10) )); In sub-classes, you can use :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::addAllowedValues` diff --git a/components/validator.rst b/components/validator.rst index 085c77a7946..12c61507257 100644 --- a/components/validator.rst +++ b/components/validator.rst @@ -36,7 +36,7 @@ characters long:: $validator = Validation::createValidator(); $violations = $validator->validate('Bernhard', [ - new Length(['min' => 10]), + new Length(min: 10), new NotBlank(), ]); diff --git a/components/validator/metadata.rst b/components/validator/metadata.rst index e7df42413bc..782e1ee216f 100755 --- a/components/validator/metadata.rst +++ b/components/validator/metadata.rst @@ -24,7 +24,7 @@ the ``Author`` class has at least 3 characters:: $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); $metadata->addPropertyConstraint( 'firstName', - new Assert\Length(["min" => 3]) + new Assert\Length(min: 3) ); } } @@ -55,9 +55,9 @@ Then, add the Validator component configuration to the class:: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([ - 'message' => 'The password cannot match your first name', - ])); + $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue( + message: 'The password cannot match your first name', + )); } } diff --git a/components/validator/resources.rst b/components/validator/resources.rst index 5b1448dfba1..64bf26e44d5 100644 --- a/components/validator/resources.rst +++ b/components/validator/resources.rst @@ -42,10 +42,10 @@ In this example, the validation metadata is retrieved executing the public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); - $metadata->addPropertyConstraint('name', new Assert\Length([ - 'min' => 5, - 'max' => 20, - ])); + $metadata->addPropertyConstraint('name', new Assert\Length( + min: 5, + max: 20, + )); } } diff --git a/controller/upload_file.rst b/controller/upload_file.rst index b3dc2d6ffd0..cff326a8e2b 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -75,14 +75,14 @@ so Symfony doesn't try to get/set its value from the related entity:: // unmapped fields can't define their validation using attributes // in the associated entity, so you can use the PHP constraint classes 'constraints' => [ - new File([ - 'maxSize' => '1024k', - 'mimeTypes' => [ + new File( + maxSize: '1024k', + mimeTypes: [ 'application/pdf', 'application/x-pdf', ], - 'mimeTypesMessage' => 'Please upload a valid PDF document', - ]) + mimeTypesMessage: 'Please upload a valid PDF document', + ) ], ]) // ... diff --git a/form/without_class.rst b/form/without_class.rst index 8b0af7cf23f..5fec7f3a663 100644 --- a/form/without_class.rst +++ b/form/without_class.rst @@ -96,12 +96,12 @@ but here's a short example:: { $builder ->add('firstName', TextType::class, [ - 'constraints' => new Length(['min' => 3]), + 'constraints' => new Length(min: 3), ]) ->add('lastName', TextType::class, [ 'constraints' => [ new NotBlank(), - new Length(['min' => 3]), + new Length(min: 3), ], ]) ; @@ -153,10 +153,10 @@ This can be done by setting the ``constraints`` option in the $resolver->setDefaults([ 'data_class' => null, 'constraints' => new Collection([ - 'firstName' => new Length(['min' => 3]), + 'firstName' => new Length(min: 3), 'lastName' => [ new NotBlank(), - new Length(['min' => 3]), + new Length(min: 3), ], ]), ]); diff --git a/validation.rst b/validation.rst index 4905283b18b..5d420ed45f7 100644 --- a/validation.rst +++ b/validation.rst @@ -327,99 +327,15 @@ literature genre mostly associated with the author, which can be set to either { // ... - $metadata->addPropertyConstraint('genre', new Assert\Choice([ - 'choices' => ['fiction', 'non-fiction'], - 'message' => 'Choose a valid genre.', - ])); + $metadata->addPropertyConstraint('genre', new Assert\Choice( + choices: ['fiction', 'non-fiction'], + message: 'Choose a valid genre.', + )); } } .. _validation-default-option: -The options of a constraint can always be passed in as an array. Some constraints, -however, also allow you to pass the value of one, "*default*", option in place -of the array. In the case of the ``Choice`` constraint, the ``choices`` -options can be specified in this way. - -.. configuration-block:: - - .. code-block:: php-attributes - - // src/Entity/Author.php - namespace App\Entity; - - // ... - use Symfony\Component\Validator\Constraints as Assert; - - class Author - { - #[Assert\Choice(['fiction', 'non-fiction'])] - private string $genre; - - // ... - } - - .. code-block:: yaml - - # config/validator/validation.yaml - App\Entity\Author: - properties: - genre: - - Choice: [fiction, non-fiction] - # ... - - .. code-block:: xml - - - - - - - - - fiction - non-fiction - - - - - - - - .. code-block:: php - - // src/Entity/Author.php - namespace App\Entity; - - // ... - use Symfony\Component\Validator\Constraints as Assert; - use Symfony\Component\Validator\Mapping\ClassMetadata; - - class Author - { - private string $genre; - - public static function loadValidatorMetadata(ClassMetadata $metadata): void - { - // ... - - $metadata->addPropertyConstraint( - 'genre', - new Assert\Choice(['fiction', 'non-fiction']) - ); - } - } - -This is purely meant to make the configuration of the most common option of -a constraint shorter and quicker. - -If you're ever unsure of how to specify an option, either check the namespace -``Symfony\Component\Validator\Constraints`` for the constraint or play it safe -by always passing in an array of options (the first method shown above). - Constraints in Form Classes --------------------------- @@ -520,7 +436,7 @@ class to have at least 3 characters. $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); $metadata->addPropertyConstraint( 'firstName', - new Assert\Length(['min' => 3]) + new Assert\Length(min: 3) ); } } @@ -603,9 +519,9 @@ this method must return ``true``: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([ - 'message' => 'The password cannot match your first name', - ])); + $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue( + message: 'The password cannot match your first name', + )); } } diff --git a/validation/custom_constraint.rst b/validation/custom_constraint.rst index 08ed86fb4ce..bb34775a71c 100644 --- a/validation/custom_constraint.rst +++ b/validation/custom_constraint.rst @@ -248,7 +248,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 ContainsAlphanumeric(['mode' => 'loose'])); + $metadata->addPropertyConstraint('name', new ContainsAlphanumeric(mode: 'loose')); } } @@ -273,6 +273,7 @@ define those options as public properties on the constraint class:: // src/Validator/Foo.php namespace App\Validator; + use Symfony\Component\Validator\Attribute\HasNamedArguments; use Symfony\Component\Validator\Constraint; #[\Attribute] @@ -282,6 +283,7 @@ define those options as public properties on the constraint class:: public $message = 'This value is invalid'; public $optionalBarOption = false; + #[HasNamedArguments] public function __construct( $mandatoryFooOption, ?string $message = null, @@ -402,10 +404,10 @@ 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 Foo([ - 'mandatoryFooOption' => 'bar', - 'optionalBarOption' => true, - ])); + $metadata->addPropertyConstraint('name', new Foo( + mandatoryFooOption: 'bar', + optionalBarOption: true, + )); } } diff --git a/validation/groups.rst b/validation/groups.rst index 8d84e52c0da..55625be702d 100644 --- a/validation/groups.rst +++ b/validation/groups.rst @@ -101,21 +101,21 @@ user registers and when a user updates their contact information later: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('email', new Assert\Email([ - 'groups' => ['registration'], - ])); - - $metadata->addPropertyConstraint('password', new Assert\NotBlank([ - 'groups' => ['registration'], - ])); - $metadata->addPropertyConstraint('password', new Assert\Length([ - 'min' => 7, - 'groups' => ['registration'], - ])); - - $metadata->addPropertyConstraint('city', new Assert\Length([ - 'min' => 2, - ])); + $metadata->addPropertyConstraint('email', new Assert\Email( + groups: ['registration'], + )); + + $metadata->addPropertyConstraint('password', new Assert\NotBlank( + groups: ['registration'], + )); + $metadata->addPropertyConstraint('password', new Assert\Length( + min: 7, + groups: ['registration'], + )); + + $metadata->addPropertyConstraint('city', new Assert\Length( + min: 2, + )); } } diff --git a/validation/sequence_provider.rst b/validation/sequence_provider.rst index 836568c2327..c316a85d249 100644 --- a/validation/sequence_provider.rst +++ b/validation/sequence_provider.rst @@ -104,10 +104,10 @@ username and the password are different only if all other validation passes $metadata->addPropertyConstraint('username', new Assert\NotBlank()); $metadata->addPropertyConstraint('password', new Assert\NotBlank()); - $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue([ - 'message' => 'The password cannot match your first name', - 'groups' => ['Strict'], - ])); + $metadata->addGetterConstraint('passwordSafe', new Assert\IsTrue( + message: 'The password cannot match your first name', + groups: ['Strict'], + )); $metadata->setGroupSequence(['User', 'Strict']); } @@ -249,10 +249,10 @@ entity and a new constraint group called ``Premium``: public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('name', new Assert\NotBlank()); - $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme([ - 'schemes' => [Assert\CardScheme::VISA], - 'groups' => ['Premium'], - ])); + $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme( + schemes: [Assert\CardScheme::VISA], + groups: ['Premium'], + )); } } diff --git a/validation/severity.rst b/validation/severity.rst index 632a99519d9..154c13d5e3e 100644 --- a/validation/severity.rst +++ b/validation/severity.rst @@ -105,15 +105,15 @@ Use the ``payload`` option to configure the error level for each constraint: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('username', new Assert\NotBlank([ - 'payload' => ['severity' => 'error'], - ])); - $metadata->addPropertyConstraint('password', new Assert\NotBlank([ - 'payload' => ['severity' => 'error'], - ])); - $metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban([ - 'payload' => ['severity' => 'warning'], - ])); + $metadata->addPropertyConstraint('username', new Assert\NotBlank( + payload: ['severity' => 'error'], + )); + $metadata->addPropertyConstraint('password', new Assert\NotBlank( + payload: ['severity' => 'error'], + )); + $metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban( + payload: ['severity' => 'warning'], + )); } } diff --git a/validation/translations.rst b/validation/translations.rst index 9a4ece17736..db2cd518eb7 100644 --- a/validation/translations.rst +++ b/validation/translations.rst @@ -83,9 +83,9 @@ property is not empty, add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('name', new NotBlank([ - 'message' => 'author.name.not_blank', - ])); + $metadata->addPropertyConstraint('name', new NotBlank( + message: 'author.name.not_blank', + )); } } @@ -136,13 +136,13 @@ You can also use :class:`Symfony\\Component\\Translation\\TranslatableMessage` t use Symfony\Component\Translation\TranslatableMessage; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Context\ExecutionContextInterface; - + #[Assert\Callback] public function validate(ExecutionContextInterface $context, mixed $payload): void { // somehow you have an array of "fake names" $fakeNames = [/* ... */]; - + // check if the name is actually a fake name if (in_array($this->getFirstName(), $fakeNames, true)) { $context->buildViolation(new TranslatableMessage('author.name.fake', [], 'validators'))