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/reference/constraints/All.rst b/reference/constraints/All.rst index 3aa05b1d2d0..0a008676f01 100644 --- a/reference/constraints/All.rst +++ b/reference/constraints/All.rst @@ -82,7 +82,7 @@ entry in that array: $metadata->addPropertyConstraint('favoriteColors', new Assert\All([ 'constraints' => [ new Assert\NotBlank(), - new Assert\Length(['min' => 5]), + new Assert\Length(min: 5), ], ])); } diff --git a/reference/constraints/AtLeastOneOf.rst b/reference/constraints/AtLeastOneOf.rst index 0a6ab618aa5..f313e182cff 100644 --- a/reference/constraints/AtLeastOneOf.rst +++ b/reference/constraints/AtLeastOneOf.rst @@ -117,19 +117,19 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([ 'constraints' => [ - new Assert\Regex(['pattern' => '/#/']), - new Assert\Length(['min' => 10]), + new Assert\Regex(pattern: '/#/'), + new Assert\Length(min: 10), ], ])); $metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf([ 'constraints' => [ - new Assert\Count(['min' => 3]), - new Assert\All([ - 'constraints' => [ + new Assert\Count(min: 3), + new Assert\All( + 'constraints: [ new Assert\GreaterThanOrEqual(5), ], - ]), + ), ], ])); } diff --git a/reference/constraints/CardScheme.rst b/reference/constraints/CardScheme.rst index 6e98e6fab98..bb3e9e797ad 100644 --- a/reference/constraints/CardScheme.rst +++ b/reference/constraints/CardScheme.rst @@ -77,12 +77,12 @@ on an object that will contain a credit card number. { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme([ - 'schemes' => [ + $metadata->addPropertyConstraint('cardNumber', new Assert\CardScheme( + schemes: [ Assert\CardScheme::VISA, ], - 'message' => 'Your credit card number is invalid.', - ])); + message: 'Your credit card number is invalid.', + )); } } diff --git a/reference/constraints/Compound.rst b/reference/constraints/Compound.rst index 0d0dc933ae0..4d2c7743176 100644 --- a/reference/constraints/Compound.rst +++ b/reference/constraints/Compound.rst @@ -35,9 +35,9 @@ you can create your own named set or requirements to be reused consistently ever return [ new Assert\NotBlank(), new Assert\Type('string'), - new Assert\Length(['min' => 12]), + new Assert\Length(min: 12), new Assert\NotCompromisedPassword(), - new Assert\PasswordStrength(['minScore' => 4]), + new Assert\PasswordStrength(minScore: 4), ]; } } diff --git a/reference/constraints/Count.rst b/reference/constraints/Count.rst index 0bf40aca8e9..d33c54c0812 100644 --- a/reference/constraints/Count.rst +++ b/reference/constraints/Count.rst @@ -82,12 +82,12 @@ you might add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('emails', new Assert\Count([ - 'min' => 1, - 'max' => 5, - 'minMessage' => 'You must specify at least one email', - 'maxMessage' => 'You cannot specify more than {{ limit }} emails', - ])); + $metadata->addPropertyConstraint('emails', new Assert\Count( + min: 1, + max: 5, + minMessage: 'You must specify at least one email', + maxMessage: 'You cannot specify more than {{ limit }} emails', + )); } } diff --git a/reference/constraints/CssColor.rst b/reference/constraints/CssColor.rst index 88a4eb4be9f..b617ebf1c42 100644 --- a/reference/constraints/CssColor.rst +++ b/reference/constraints/CssColor.rst @@ -110,15 +110,15 @@ the named CSS colors: { $metadata->addPropertyConstraint('defaultColor', new Assert\CssColor()); - $metadata->addPropertyConstraint('accentColor', new Assert\CssColor([ - 'formats' => Assert\CssColor::HEX_LONG, - 'message' => 'The accent color must be a 6-character hexadecimal color.', - ])); - - $metadata->addPropertyConstraint('currentColor', new Assert\CssColor([ - 'formats' => [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS], - 'message' => 'The color "{{ value }}" is not a valid CSS color name.', - ])); + $metadata->addPropertyConstraint('accentColor', new Assert\CssColor( + formats: Assert\CssColor::HEX_LONG, + message: 'The accent color must be a 6-character hexadecimal color.', + )); + + $metadata->addPropertyConstraint('currentColor', new Assert\CssColor( + formats: [Assert\CssColor::BASIC_NAMED_COLORS, Assert\CssColor::EXTENDED_NAMED_COLORS], + message: 'The color "{{ value }}" is not a valid CSS color name.', + )); } } diff --git a/reference/constraints/DivisibleBy.rst b/reference/constraints/DivisibleBy.rst index dd90ad9a0fd..23b36023cff 100644 --- a/reference/constraints/DivisibleBy.rst +++ b/reference/constraints/DivisibleBy.rst @@ -92,9 +92,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('weight', new Assert\DivisibleBy(0.25)); - $metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy([ - 'value' => 5, - ])); + $metadata->addPropertyConstraint('quantity', new Assert\DivisibleBy( + value: 5, + )); } } diff --git a/reference/constraints/Email.rst b/reference/constraints/Email.rst index 516d6d07dca..41012e5e935 100644 --- a/reference/constraints/Email.rst +++ b/reference/constraints/Email.rst @@ -70,9 +70,9 @@ Basic Usage public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('email', new Assert\Email([ - 'message' => 'The email "{{ value }}" is not a valid email.', - ])); + $metadata->addPropertyConstraint('email', new Assert\Email( + message: 'The email "{{ value }}" is not a valid email.', + )); } } diff --git a/reference/constraints/EqualTo.rst b/reference/constraints/EqualTo.rst index d5d78f60a0f..fdc402b1a97 100644 --- a/reference/constraints/EqualTo.rst +++ b/reference/constraints/EqualTo.rst @@ -91,9 +91,9 @@ and that the ``age`` is ``20``, you could do the following: { $metadata->addPropertyConstraint('firstName', new Assert\EqualTo('Mary')); - $metadata->addPropertyConstraint('age', new Assert\EqualTo([ - 'value' => 20, - ])); + $metadata->addPropertyConstraint('age', new Assert\EqualTo( + value: 20, + )); } } diff --git a/reference/constraints/ExpressionSyntax.rst b/reference/constraints/ExpressionSyntax.rst index c1d086790c1..37e0ad7de4a 100644 --- a/reference/constraints/ExpressionSyntax.rst +++ b/reference/constraints/ExpressionSyntax.rst @@ -90,9 +90,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('promotion', new Assert\ExpressionSyntax()); - $metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax([ - 'allowedVariables' => ['user', 'shipping_centers'], - ])); + $metadata->addPropertyConstraint('shippingOptions', new Assert\ExpressionSyntax( + allowedVariables: ['user', 'shipping_centers'], + )); } } diff --git a/reference/constraints/File.rst b/reference/constraints/File.rst index 6d9b72d17b8..495c19f9cbe 100644 --- a/reference/constraints/File.rst +++ b/reference/constraints/File.rst @@ -119,13 +119,13 @@ below a certain file size and a valid PDF, add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('bioFile', new Assert\File([ - 'maxSize' => '1024k', - 'extensions' => [ + $metadata->addPropertyConstraint('bioFile', new Assert\File( + maxSize: '1024k', + extensions: [ 'pdf', ], - 'extensionsMessage' => 'Please upload a valid PDF', - ])); + extensionsMessage: 'Please upload a valid PDF', + )); } } diff --git a/reference/constraints/Hostname.rst b/reference/constraints/Hostname.rst index 95b10d1736e..58ac0364669 100644 --- a/reference/constraints/Hostname.rst +++ b/reference/constraints/Hostname.rst @@ -72,9 +72,9 @@ will contain a host name. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('name', new Assert\Hostname([ - 'message' => 'The server name must be a valid hostname.', - ])); + $metadata->addPropertyConstraint('name', new Assert\Hostname( + message: 'The server name must be a valid hostname.', + )); } } diff --git a/reference/constraints/Iban.rst b/reference/constraints/Iban.rst index 3cf800200e2..8d5982eea6d 100644 --- a/reference/constraints/Iban.rst +++ b/reference/constraints/Iban.rst @@ -77,9 +77,9 @@ will contain an International Bank Account Number. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban([ - 'message' => 'This is not a valid International Bank Account Number (IBAN).', - ])); + $metadata->addPropertyConstraint('bankAccountNumber', new Assert\Iban( + message: 'This is not a valid International Bank Account Number (IBAN).', + )); } } diff --git a/reference/constraints/IdenticalTo.rst b/reference/constraints/IdenticalTo.rst index 5b6d853dc0b..f8844f90a72 100644 --- a/reference/constraints/IdenticalTo.rst +++ b/reference/constraints/IdenticalTo.rst @@ -94,9 +94,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('firstName', new Assert\IdenticalTo('Mary')); - $metadata->addPropertyConstraint('age', new Assert\IdenticalTo([ - 'value' => 20, - ])); + $metadata->addPropertyConstraint('age', new Assert\IdenticalTo( + value: 20, + )); } } diff --git a/reference/constraints/IsFalse.rst b/reference/constraints/IsFalse.rst index 0b9ebe77491..3d0a1665944 100644 --- a/reference/constraints/IsFalse.rst +++ b/reference/constraints/IsFalse.rst @@ -93,9 +93,9 @@ method returns **false**: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse([ - 'message' => "You've entered an invalid state.", - ])); + $metadata->addGetterConstraint('stateInvalid', new Assert\IsFalse( + message: "You've entered an invalid state.", + )); } public function isStateInvalid(): bool diff --git a/reference/constraints/IsTrue.rst b/reference/constraints/IsTrue.rst index 678371f6e69..b50ba4f3e8b 100644 --- a/reference/constraints/IsTrue.rst +++ b/reference/constraints/IsTrue.rst @@ -97,9 +97,9 @@ Then you can validate this method with ``IsTrue`` as follows: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addGetterConstraint('tokenValid', new IsTrue([ - 'message' => 'The token is invalid.', - ])); + $metadata->addGetterConstraint('tokenValid', new IsTrue( + message: 'The token is invalid.', + )); } public function isTokenValid(): bool diff --git a/reference/constraints/Isbn.rst b/reference/constraints/Isbn.rst index 954bff233d5..52d10565fe5 100644 --- a/reference/constraints/Isbn.rst +++ b/reference/constraints/Isbn.rst @@ -76,10 +76,10 @@ on an object that will contain an ISBN. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('isbn', new Assert\Isbn([ - 'type' => Assert\Isbn::ISBN_10, - 'message' => 'This value is not valid.', - ])); + $metadata->addPropertyConstraint('isbn', new Assert\Isbn( + type: Assert\Isbn::ISBN_10, + message: 'This value is not valid.', + )); } } diff --git a/reference/constraints/Json.rst b/reference/constraints/Json.rst index 28e15976f3c..337b2dc6a1e 100644 --- a/reference/constraints/Json.rst +++ b/reference/constraints/Json.rst @@ -69,9 +69,9 @@ The ``Json`` constraint can be applied to a property or a "getter" method: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('chapters', new Assert\Json([ - 'message' => 'You\'ve entered an invalid Json.', - ])); + $metadata->addPropertyConstraint('chapters', new Assert\Json( + message: 'You\'ve entered an invalid Json.', + )); } } diff --git a/reference/constraints/Length.rst b/reference/constraints/Length.rst index 9a4478f509b..c1a8575070b 100644 --- a/reference/constraints/Length.rst +++ b/reference/constraints/Length.rst @@ -85,12 +85,12 @@ and ``50``, you might add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('firstName', new Assert\Length([ - 'min' => 2, - 'max' => 50, - 'minMessage' => 'Your first name must be at least {{ limit }} characters long', - 'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters', - ])); + $metadata->addPropertyConstraint('firstName', new Assert\Length( + min: 2, + max: 50, + minMessage: 'Your first name must be at least {{ limit }} characters long', + maxMessage: 'Your first name cannot be longer than {{ limit }} characters', + )); } } diff --git a/reference/constraints/Type.rst b/reference/constraints/Type.rst index b99e8ce1c54..584bb047d4d 100644 --- a/reference/constraints/Type.rst +++ b/reference/constraints/Type.rst @@ -127,14 +127,14 @@ The following example checks if ``emailAddress`` is an instance of ``Symfony\Com $metadata->addPropertyConstraint('firstName', new Assert\Type('string')); - $metadata->addPropertyConstraint('age', new Assert\Type([ - 'type' => 'integer', - 'message' => 'The value {{ value }} is not a valid {{ type }}.', - ])); - - $metadata->addPropertyConstraint('accessCode', new Assert\Type([ - 'type' => ['alpha', 'digit'], - ])); + $metadata->addPropertyConstraint('age', new Assert\Type( + type: 'integer', + message: 'The value {{ value }} is not a valid {{ type }}.', + )); + + $metadata->addPropertyConstraint('accessCode', new Assert\Type( + type: ['alpha', 'digit'], + )); } } diff --git a/reference/constraints/Week.rst b/reference/constraints/Week.rst index f107d8b4192..b3c1b0ca122 100644 --- a/reference/constraints/Week.rst +++ b/reference/constraints/Week.rst @@ -79,10 +79,10 @@ the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('startWeek', new Assert\Week([ - 'min' => '2022-W01', - 'max' => '2022-W20', - ])); + $metadata->addPropertyConstraint('startWeek', new Assert\Week( + min: '2022-W01', + max: '2022-W20', + )); } } diff --git a/reference/constraints/WordCount.rst b/reference/constraints/WordCount.rst index 74c79216898..392f8a5bcb7 100644 --- a/reference/constraints/WordCount.rst +++ b/reference/constraints/WordCount.rst @@ -78,10 +78,10 @@ class contains between 100 and 200 words, you could do the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('content', new Assert\WordCount([ - 'min' => 100, - 'max' => 200, - ])); + $metadata->addPropertyConstraint('content', new Assert\WordCount( + min: 100, + max: 200, + )); } } 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'))