From 02052a8d2ec2058f32c7588454fe47de64206899 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Fri, 24 Jan 2025 11:36:59 +0100 Subject: [PATCH] use named-arguments to configure validation constraint options --- components/console/helpers/questionhelper.rst | 8 +- components/options_resolver.rst | 2 +- components/validator.rst | 2 +- components/validator/metadata.rst | 8 +- components/validator/resources.rst | 8 +- controller/upload_file.rst | 10 +- form/without_class.rst | 8 +- reference/constraints/All.rst | 8 +- reference/constraints/AtLeastOneOf.rst | 24 ++--- reference/constraints/CardScheme.rst | 8 +- reference/constraints/Choice.rst | 20 ++-- reference/constraints/Collection.rst | 44 ++++---- reference/constraints/Compound.rst | 4 +- reference/constraints/Count.rst | 12 +-- reference/constraints/CssColor.rst | 18 ++-- reference/constraints/DivisibleBy.rst | 6 +- reference/constraints/Email.rst | 6 +- reference/constraints/EqualTo.rst | 6 +- reference/constraints/Expression.rst | 24 ++--- reference/constraints/ExpressionSyntax.rst | 6 +- reference/constraints/File.rst | 10 +- reference/constraints/GreaterThan.rst | 6 +- reference/constraints/GreaterThanOrEqual.rst | 6 +- reference/constraints/Hostname.rst | 6 +- reference/constraints/Iban.rst | 6 +- reference/constraints/IdenticalTo.rst | 6 +- reference/constraints/Image.rst | 20 ++-- reference/constraints/IsFalse.rst | 6 +- reference/constraints/IsTrue.rst | 6 +- reference/constraints/Isbn.rst | 8 +- reference/constraints/Json.rst | 6 +- reference/constraints/Length.rst | 12 +-- reference/constraints/LessThan.rst | 6 +- reference/constraints/LessThanOrEqual.rst | 6 +- reference/constraints/Locale.rst | 6 +- reference/constraints/Luhn.rst | 6 +- reference/constraints/NotEqualTo.rst | 6 +- reference/constraints/NotIdenticalTo.rst | 6 +- reference/constraints/PasswordStrength.rst | 12 +-- reference/constraints/Range.rst | 34 +++--- reference/constraints/Regex.rst | 24 ++--- reference/constraints/Sequentially.rst | 2 +- reference/constraints/Type.rst | 16 +-- reference/constraints/Unique.rst | 6 +- reference/constraints/UniqueEntity.rst | 14 +-- reference/constraints/Url.rst | 26 ++--- reference/constraints/Valid.rst | 4 +- reference/constraints/Week.rst | 8 +- reference/constraints/When.rst | 24 ++--- reference/constraints/WordCount.rst | 8 +- reference/constraints/Yaml.rst | 14 +-- validation.rst | 102 ++---------------- validation/custom_constraint.rst | 12 ++- validation/groups.rst | 30 +++--- validation/sequence_provider.rst | 16 +-- validation/severity.rst | 18 ++-- validation/translations.rst | 10 +- 57 files changed, 331 insertions(+), 415 deletions(-) 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..bfcea0dd1b5 100644 --- a/reference/constraints/All.rst +++ b/reference/constraints/All.rst @@ -79,12 +79,12 @@ entry in that array: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('favoriteColors', new Assert\All([ - 'constraints' => [ + $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..5c568987ec4 100644 --- a/reference/constraints/AtLeastOneOf.rst +++ b/reference/constraints/AtLeastOneOf.rst @@ -115,23 +115,23 @@ The following constraints ensure that: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf([ - 'constraints' => [ - new Assert\Regex(['pattern' => '/#/']), - new Assert\Length(['min' => 10]), + $metadata->addPropertyConstraint('password', new Assert\AtLeastOneOf( + constraints: [ + 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' => [ + $metadata->addPropertyConstraint('grades', new Assert\AtLeastOneOf( + 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/Choice.rst b/reference/constraints/Choice.rst index 5a9c365be37..bb7150adc8f 100644 --- a/reference/constraints/Choice.rst +++ b/reference/constraints/Choice.rst @@ -100,10 +100,10 @@ If your valid choice list is simple, you can pass them in directly via the new Assert\Choice(['New York', 'Berlin', 'Tokyo']) ); - $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.', + )); } } @@ -182,9 +182,9 @@ constraint. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('genre', new Assert\Choice([ - 'callback' => 'getGenres', - ])); + $metadata->addPropertyConstraint('genre', new Assert\Choice( + callback: 'getGenres', + )); } } @@ -250,9 +250,9 @@ you can pass the class name and the method as an array. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('genre', new Assert\Choice([ - 'callback' => [Genre::class, 'getGenres'], - ])); + $metadata->addPropertyConstraint('genre', new Assert\Choice( + callback: [Genre::class, 'getGenres'], + )); } } diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst index 2d16d201b17..557c6c02380 100644 --- a/reference/constraints/Collection.rst +++ b/reference/constraints/Collection.rst @@ -139,8 +139,8 @@ following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('profileData', new Assert\Collection([ - 'fields' => [ + $metadata->addPropertyConstraint('profileData', new Assert\Collection( + fields: [ 'personal_email' => new Assert\Email(), 'short_bio' => [ new Assert\NotBlank(), @@ -150,8 +150,8 @@ following: ]), ], ], - 'allowMissingFields' => true, - ])); + allowMissingFields: true, + )); } } @@ -267,15 +267,15 @@ you can do the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('profileData', new Assert\Collection([ - 'fields' => [ + $metadata->addPropertyConstraint('profileData', new Assert\Collection( + fields: [ 'personal_email' => new Assert\Required([ new Assert\NotBlank(), new Assert\Email(), ]), 'alternate_email' => new Assert\Optional(new Assert\Email()), ], - ])); + )); } } @@ -291,28 +291,28 @@ groups. Take the following example:: use Symfony\Component\Validator\Constraints as Assert; - $constraint = new Assert\Collection([ - 'fields' => [ + $constraint = new Assert\Collection( + fields: [ 'name' => new Assert\NotBlank(['groups' => 'basic']), 'email' => new Assert\NotBlank(['groups' => 'contact']), ], - ]); + ); This will result in the following configuration:: - $constraint = new Assert\Collection([ - 'fields' => [ - 'name' => new Assert\Required([ - 'constraints' => new Assert\NotBlank(['groups' => 'basic']), - 'groups' => ['basic', 'strict'], - ]), - 'email' => new Assert\Required([ - "constraints" => new Assert\NotBlank(['groups' => 'contact']), - 'groups' => ['basic', 'strict'], - ]), + $constraint = new Assert\Collection( + fields: [ + 'name' => new Assert\Required( + constraints: new Assert\NotBlank(groups: ['basic']), + groups: ['basic', 'strict'], + ), + 'email' => new Assert\Required( + constraints: new Assert\NotBlank(groups: ['contact']), + groups: ['basic', 'strict'], + ), ], - 'groups' => ['basic', 'strict'], - ]); + groups: ['basic', 'strict'], + ); The default ``allowMissingFields`` option requires the fields in all groups. So when validating in ``contact`` group, ``$name`` can be empty but the key is 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/Expression.rst b/reference/constraints/Expression.rst index bf015d17573..d5a78f71d5e 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -111,10 +111,10 @@ One way to accomplish this is with the Expression constraint: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addConstraint(new Assert\Expression([ - 'expression' => 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()', - 'message' => 'If this is a tech post, the category should be either php or symfony!', - ])); + $metadata->addConstraint(new Assert\Expression( + expression: 'this.getCategory() in ["php", "symfony"] or !this.isTechnicalPost()', + message: 'If this is a tech post, the category should be either php or symfony!', + )); } // ... @@ -200,10 +200,10 @@ assert that the expression must return ``true`` for validation to fail. { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression([ - 'expression' => 'this.getCategory() in ["php", "symfony"] or value == false', - 'message' => 'If this is a tech post, the category should be either php or symfony!', - ])); + $metadata->addPropertyConstraint('isTechnicalPost', new Assert\Expression( + expression: 'this.getCategory() in ["php", "symfony"] or value == false', + message: 'If this is a tech post, the category should be either php or symfony!', + )); } // ... @@ -343,10 +343,10 @@ type (numeric, boolean, strings, null, etc.) { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('metric', new Assert\Expression([ - 'expression' => 'value + error_margin < threshold', - 'values' => ['error_margin' => 0.25, 'threshold' => 1.5], - ])); + $metadata->addPropertyConstraint('metric', new Assert\Expression( + expression: 'value + error_margin < threshold', + values: ['error_margin' => 0.25, 'threshold' => 1.5], + )); } // ... 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/GreaterThan.rst b/reference/constraints/GreaterThan.rst index 4f2e34bcbfa..d1b79028acd 100644 --- a/reference/constraints/GreaterThan.rst +++ b/reference/constraints/GreaterThan.rst @@ -89,9 +89,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('siblings', new Assert\GreaterThan(5)); - $metadata->addPropertyConstraint('age', new Assert\GreaterThan([ - 'value' => 18, - ])); + $metadata->addPropertyConstraint('age', new Assert\GreaterThan( + value: 18, + )); } } diff --git a/reference/constraints/GreaterThanOrEqual.rst b/reference/constraints/GreaterThanOrEqual.rst index e5a71e5f788..63c2ade6197 100644 --- a/reference/constraints/GreaterThanOrEqual.rst +++ b/reference/constraints/GreaterThanOrEqual.rst @@ -88,9 +88,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('siblings', new Assert\GreaterThanOrEqual(5)); - $metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual([ - 'value' => 18, - ])); + $metadata->addPropertyConstraint('age', new Assert\GreaterThanOrEqual( + value: 18, + )); } } 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/Image.rst b/reference/constraints/Image.rst index 23fbe5a157a..5dd270c44f8 100644 --- a/reference/constraints/Image.rst +++ b/reference/constraints/Image.rst @@ -116,12 +116,12 @@ that it is between a certain size, add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('headshot', new Assert\Image([ - 'minWidth' => 200, - 'maxWidth' => 400, - 'minHeight' => 200, - 'maxHeight' => 400, - ])); + $metadata->addPropertyConstraint('headshot', new Assert\Image( + minWidth: 200, + maxWidth: 400, + minHeight: 200, + maxHeight: 400, + )); } } @@ -187,10 +187,10 @@ following code: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('headshot', new Assert\Image([ - 'allowLandscape' => false, - 'allowPortrait' => false, - ])); + $metadata->addPropertyConstraint('headshot', new Assert\Image( + allowLandscape: false, + allowPortrait: false, + )); } } 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/LessThan.rst b/reference/constraints/LessThan.rst index 964bfbb3527..3d23bcda445 100644 --- a/reference/constraints/LessThan.rst +++ b/reference/constraints/LessThan.rst @@ -89,9 +89,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('siblings', new Assert\LessThan(5)); - $metadata->addPropertyConstraint('age', new Assert\LessThan([ - 'value' => 80, - ])); + $metadata->addPropertyConstraint('age', new Assert\LessThan( + value: 80, + )); } } diff --git a/reference/constraints/LessThanOrEqual.rst b/reference/constraints/LessThanOrEqual.rst index 9420c3e4376..ac66c62d7d0 100644 --- a/reference/constraints/LessThanOrEqual.rst +++ b/reference/constraints/LessThanOrEqual.rst @@ -88,9 +88,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('siblings', new Assert\LessThanOrEqual(5)); - $metadata->addPropertyConstraint('age', new Assert\LessThanOrEqual([ - 'value' => 80, - ])); + $metadata->addPropertyConstraint('age', new Assert\LessThanOrEqual( + value: 80, + )); } } diff --git a/reference/constraints/Locale.rst b/reference/constraints/Locale.rst index 49edd473d05..4bba45ae12b 100644 --- a/reference/constraints/Locale.rst +++ b/reference/constraints/Locale.rst @@ -78,9 +78,9 @@ Basic Usage public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('locale', new Assert\Locale([ - 'canonicalize' => true, - ])); + $metadata->addPropertyConstraint('locale', new Assert\Locale( + canonicalize: true, + )); } } diff --git a/reference/constraints/Luhn.rst b/reference/constraints/Luhn.rst index 8f5ef34c4ba..0c835204091 100644 --- a/reference/constraints/Luhn.rst +++ b/reference/constraints/Luhn.rst @@ -72,9 +72,9 @@ will contain a credit card number. public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('cardNumber', new Assert\Luhn([ - 'message' => 'Please check your credit card number', - ])); + $metadata->addPropertyConstraint('cardNumber', new Assert\Luhn( + message: 'Please check your credit card number', + )); } } diff --git a/reference/constraints/NotEqualTo.rst b/reference/constraints/NotEqualTo.rst index b8ee4cac32f..dd3f633b4a1 100644 --- a/reference/constraints/NotEqualTo.rst +++ b/reference/constraints/NotEqualTo.rst @@ -93,9 +93,9 @@ the following: { $metadata->addPropertyConstraint('firstName', new Assert\NotEqualTo('Mary')); - $metadata->addPropertyConstraint('age', new Assert\NotEqualTo([ - 'value' => 15, - ])); + $metadata->addPropertyConstraint('age', new Assert\NotEqualTo( + value: 15, + )); } } diff --git a/reference/constraints/NotIdenticalTo.rst b/reference/constraints/NotIdenticalTo.rst index 9ea93dc4b86..b2c20027292 100644 --- a/reference/constraints/NotIdenticalTo.rst +++ b/reference/constraints/NotIdenticalTo.rst @@ -94,9 +94,9 @@ The following constraints ensure that: { $metadata->addPropertyConstraint('age', new Assert\NotIdenticalTo('Mary')); - $metadata->addPropertyConstraint('age', new Assert\NotIdenticalTo([ - 'value' => 15, - ])); + $metadata->addPropertyConstraint('age', new Assert\NotIdenticalTo( + value: 15, + )); } } diff --git a/reference/constraints/PasswordStrength.rst b/reference/constraints/PasswordStrength.rst index 60125a763a1..0b242cacf08 100644 --- a/reference/constraints/PasswordStrength.rst +++ b/reference/constraints/PasswordStrength.rst @@ -101,9 +101,9 @@ or by a custom password strength estimator. class User { - #[Assert\PasswordStrength([ - 'minScore' => PasswordStrength::STRENGTH_VERY_STRONG, // Very strong password required - ])] + #[Assert\PasswordStrength( + minScore: PasswordStrength::STRENGTH_VERY_STRONG, // Very strong password required + )] protected $rawPassword; } @@ -123,9 +123,9 @@ The default message supplied when the password does not reach the minimum requir class User { - #[Assert\PasswordStrength([ - 'message' => 'Your password is too easy to guess. Company\'s security policy requires to use a stronger password.' - ])] + #[Assert\PasswordStrength( + message: 'Your password is too easy to guess. Company\'s security policy requires to use a stronger password.' + )] protected $rawPassword; } diff --git a/reference/constraints/Range.rst b/reference/constraints/Range.rst index edd199c48b9..46a9e3799b3 100644 --- a/reference/constraints/Range.rst +++ b/reference/constraints/Range.rst @@ -78,11 +78,11 @@ you might add the following: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('height', new Assert\Range([ - 'min' => 120, - 'max' => 180, - 'notInRangeMessage' => 'You must be between {{ min }}cm and {{ max }}cm tall to enter', - ])); + $metadata->addPropertyConstraint('height', new Assert\Range( + min: 120, + max: 180, + notInRangeMessage: 'You must be between {{ min }}cm and {{ max }}cm tall to enter', + )); } } @@ -154,10 +154,10 @@ date must lie within the current year like this: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('startDate', new Assert\Range([ - 'min' => 'first day of January', - 'max' => 'first day of January next year', - ])); + $metadata->addPropertyConstraint('startDate', new Assert\Range( + min: 'first day of January', + max: 'first day of January next year', + )); } } @@ -224,10 +224,10 @@ dates. If you want to fix the timezone, append it to the date string: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('startDate', new Assert\Range([ - 'min' => 'first day of January UTC', - 'max' => 'first day of January next year UTC', - ])); + $metadata->addPropertyConstraint('startDate', new Assert\Range( + min: 'first day of January UTC', + max: 'first day of January next year UTC', + )); } } @@ -294,10 +294,10 @@ can check that a delivery date starts within the next five hours like this: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('deliveryDate', new Assert\Range([ - 'min' => 'now', - 'max' => '+5 hours', - ])); + $metadata->addPropertyConstraint('deliveryDate', new Assert\Range( + min: 'now', + max: '+5 hours', + )); } } diff --git a/reference/constraints/Regex.rst b/reference/constraints/Regex.rst index 2e11a8d04fc..beefe786fc4 100644 --- a/reference/constraints/Regex.rst +++ b/reference/constraints/Regex.rst @@ -71,9 +71,9 @@ more word characters at the beginning of your string: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('description', new Assert\Regex([ - 'pattern' => '/^\w+/', - ])); + $metadata->addPropertyConstraint('description', new Assert\Regex( + pattern: '/^\w+/', + )); } } @@ -145,11 +145,11 @@ it a custom message: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('firstName', new Assert\Regex([ - 'pattern' => '/\d/', - 'match' => false, - 'message' => 'Your name cannot contain a number', - ])); + $metadata->addPropertyConstraint('firstName', new Assert\Regex( + pattern: '/\d/', + match: false, + message: 'Your name cannot contain a number', + )); } } @@ -236,10 +236,10 @@ need to specify the HTML5 compatible pattern in the ``htmlPattern`` option: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('name', new Assert\Regex([ - 'pattern' => '/^[a-z]+$/i', - 'htmlPattern' => '[a-zA-Z]+', - ])); + $metadata->addPropertyConstraint('name', new Assert\Regex( + pattern: '/^[a-z]+$/i', + htmlPattern: '[a-zA-Z]+', + )); } } diff --git a/reference/constraints/Sequentially.rst b/reference/constraints/Sequentially.rst index 7620997f0a3..9608898bbda 100644 --- a/reference/constraints/Sequentially.rst +++ b/reference/constraints/Sequentially.rst @@ -110,7 +110,7 @@ You can validate each of these constraints sequentially to solve these issues: $metadata->addPropertyConstraint('address', new Assert\Sequentially([ new Assert\NotNull(), new Assert\Type('string'), - new Assert\Length(['min' => 10]), + new Assert\Length(min: 10), new Assert\Regex(self::ADDRESS_REGEX), new AcmeAssert\Geolocalizable(), ])); 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/Unique.rst b/reference/constraints/Unique.rst index 68754738271..a1be67d6f2f 100644 --- a/reference/constraints/Unique.rst +++ b/reference/constraints/Unique.rst @@ -162,9 +162,9 @@ collection:: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('coordinates', new Assert\Unique([ - 'fields' => ['latitude', 'longitude'], - ])); + $metadata->addPropertyConstraint('coordinates', new Assert\Unique( + fields: ['latitude', 'longitude'], + )); } } diff --git a/reference/constraints/UniqueEntity.rst b/reference/constraints/UniqueEntity.rst index d4fbfeb8666..dc72893d7ce 100644 --- a/reference/constraints/UniqueEntity.rst +++ b/reference/constraints/UniqueEntity.rst @@ -95,9 +95,9 @@ between all of the rows in your user table: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addConstraint(new UniqueEntity([ - 'fields' => 'email', - ])); + $metadata->addConstraint(new UniqueEntity( + fields: 'email', + )); $metadata->addPropertyConstraint('email', new Assert\Email()); } @@ -346,10 +346,10 @@ this option to specify one or more fields to only ignore ``null`` values on them { public static function loadValidatorMetadata(ClassMetadata $metadata) { - $metadata->addConstraint(new UniqueEntity([ - 'fields' => ['email', 'phoneNumber'], - 'ignoreNull' => 'phoneNumber', - ])); + $metadata->addConstraint(new UniqueEntity( + fields: ['email', 'phoneNumber'], + ignoreNull: 'phoneNumber', + )); // ... } diff --git a/reference/constraints/Url.rst b/reference/constraints/Url.rst index 74f0d750dfd..fbeaa6da522 100644 --- a/reference/constraints/Url.rst +++ b/reference/constraints/Url.rst @@ -152,9 +152,9 @@ Parameter Description public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('bioUrl', new Assert\Url([ - 'message' => 'The url "{{ value }}" is not a valid url.', - ])); + $metadata->addPropertyConstraint('bioUrl', new Assert\Url( + message: 'The url "{{ value }}" is not a valid url.', + )); } } @@ -231,9 +231,9 @@ the ``ftp://`` type URLs to be valid, redefine the ``protocols`` array, listing public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('bioUrl', new Assert\Url([ - 'protocols' => ['http', 'https', 'ftp'], - ])); + $metadata->addPropertyConstraint('bioUrl', new Assert\Url( + protocols: ['http', 'https', 'ftp'], + )); } } @@ -302,9 +302,9 @@ also relative URLs that contain no protocol (e.g. ``//example.com``). public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('bioUrl', new Assert\Url([ - 'relativeProtocol' => true, - ])); + $metadata->addPropertyConstraint('bioUrl', new Assert\Url( + relativeProtocol: true, + )); } } @@ -414,10 +414,10 @@ Parameter Description public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('homepageUrl', new Assert\Url([ - 'requireTld' => true, - 'tldMessage' => 'Add at least one TLD to the {{ value }} URL.', - ])); + $metadata->addPropertyConstraint('homepageUrl', new Assert\Url( + requireTld: true, + tldMessage: 'Add at least one TLD to the {{ value }} URL.', + )); } } diff --git a/reference/constraints/Valid.rst b/reference/constraints/Valid.rst index 1f99b666419..61a2c1d992c 100644 --- a/reference/constraints/Valid.rst +++ b/reference/constraints/Valid.rst @@ -149,7 +149,7 @@ stores an ``Address`` instance in the ``$address`` property:: { $metadata->addPropertyConstraint('street', new Assert\NotBlank()); $metadata->addPropertyConstraint('zipCode', new Assert\NotBlank()); - $metadata->addPropertyConstraint('zipCode', new Assert\Length(['max' => 5])); + $metadata->addPropertyConstraint('zipCode', new Assert\Length(max: 5)); } } @@ -166,7 +166,7 @@ stores an ``Address`` instance in the ``$address`` property:: public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('firstName', new Assert\NotBlank()); - $metadata->addPropertyConstraint('firstName', new Assert\Length(['min' => 4])); + $metadata->addPropertyConstraint('firstName', new Assert\Length(min: 4)); $metadata->addPropertyConstraint('lastName', new Assert\NotBlank()); } } 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/When.rst b/reference/constraints/When.rst index 2a05e58ee9c..ec21eca9784 100644 --- a/reference/constraints/When.rst +++ b/reference/constraints/When.rst @@ -127,15 +127,15 @@ One way to accomplish this is with the When constraint: public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addPropertyConstraint('value', new Assert\GreaterThan(0)); - $metadata->addPropertyConstraint('value', new Assert\When([ - 'expression' => 'this.getType() == "percent"', - 'constraints' => [ - new Assert\LessThanOrEqual([ - 'value' => 100, - 'message' => 'The value should be between 1 and 100!', - ]), + $metadata->addPropertyConstraint('value', new Assert\When( + expression: 'this.getType() == "percent"', + constraints: [ + new Assert\LessThanOrEqual( + value: 100, + message: 'The value should be between 1 and 100!', + ), ], - ])); + )); } // ... @@ -256,12 +256,12 @@ validation based on its value: public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('type', new Assert\When([ - 'expression' => 'value == "percent"', - 'constraints' => [ + $metadata->addPropertyConstraint('type', new Assert\When( + expression: 'value == "percent"', + constraints: [ new Assert\Callback('doComplexValidation'), ], - ])); + )); } public function doComplexValidation(ExecutionContextInterface $context, $payload): void 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/reference/constraints/Yaml.rst b/reference/constraints/Yaml.rst index 49b65f251e8..0d1564f4f8a 100644 --- a/reference/constraints/Yaml.rst +++ b/reference/constraints/Yaml.rst @@ -73,9 +73,9 @@ The ``Yaml`` constraint can be applied to a property or a "getter" method: { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([ - 'message' => 'Your configuration doesn\'t have valid YAML syntax.', - ])); + $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml( + message: 'Your configuration doesn\'t have valid YAML syntax.', + )); } } @@ -122,10 +122,10 @@ Its value is a combination of one or more of the :ref:`flags defined by the Yaml { public static function loadValidatorMetadata(ClassMetadata $metadata): void { - $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([ - 'message' => 'Your configuration doesn\'t have valid YAML syntax.', - 'flags' => Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME, - ])); + $metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml( + message: 'Your configuration doesn\'t have valid YAML syntax.', + flags: Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME, + )); } } diff --git a/validation.rst b/validation.rst index 4905283b18b..cfa8154b627 100644 --- a/validation.rst +++ b/validation.rst @@ -327,99 +327,13 @@ 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 +434,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 +517,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'))