From 9bbc99f3ca2e794130f3f900c3c99e6e591f425f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 29 Oct 2012 11:06:22 +0100 Subject: [PATCH 1/3] Replace MinLength with Length --- book/forms.rst | 8 +++---- book/validation.rst | 42 ++++++++++++++++++++------------- reference/constraints/All.rst | 5 ++-- reference/constraints/Valid.rst | 13 ++++++---- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 63e66ac16c6..452aefeb846 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -1533,11 +1533,11 @@ but here's a short example:: // import the namespaces above your controller class use Symfony\Component\Validator\Constraints\Email; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Collection; $collectionConstraint = new Collection(array( - 'name' => new MinLength(5), + 'name' => new Length(array("min" => 5)), 'email' => new Email(array('message' => 'Invalid email address')), )); @@ -1558,7 +1558,7 @@ method to specify the option:: use Symfony\Component\Form\FormBuilder; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Email; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\Collection; class ContactType extends AbstractType @@ -1568,7 +1568,7 @@ method to specify the option:: public function setDefaultOptions(OptionsResolverInterface $resolver) { $collectionConstraint = new Collection(array( - 'name' => new MinLength(5), + 'name' => new Length(array("min" => 5)), 'email' => new Email(array('message' => 'Invalid email address')), )); diff --git a/book/validation.rst b/book/validation.rst index fff30e78b6c..e78d062d971 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -504,7 +504,8 @@ class to have at least 3 characters. properties: firstName: - NotBlank: ~ - - MinLength: 3 + - Length: + min: 3 .. code-block:: php-annotations @@ -517,7 +518,7 @@ class to have at least 3 characters. { /** * @Assert\NotBlank() - * @Assert\MinLength(3) + * @Assert\Length(min = "3") */ private $firstName; } @@ -528,7 +529,9 @@ class to have at least 3 characters. - 3 + + + @@ -539,7 +542,7 @@ class to have at least 3 characters. // ... use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\Length; class Author { @@ -548,7 +551,9 @@ class to have at least 3 characters. public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('firstName', new NotBlank()); - $metadata->addPropertyConstraint('firstName', new MinLength(3)); + $metadata->addPropertyConstraint( + 'firstName', + new Length(array("min" => 3))); } } @@ -677,9 +682,10 @@ user registers and when a user updates his/her contact information later: - Email: { groups: [registration] } password: - NotBlank: { groups: [registration] } - - MinLength: { limit: 7, groups: [registration] } + - Length: { min: 7, groups: [registration] } city: - - MinLength: 2 + - Length: + min: 2 .. code-block:: php-annotations @@ -698,12 +704,12 @@ user registers and when a user updates his/her contact information later: /** * @Assert\NotBlank(groups={"registration"}) - * @Assert\MinLength(limit=7, groups={"registration"}) + * @Assert\Length(min=7, groups={"registration"}) */ private $password; /** - * @Assert\MinLength(2) + * @Assert\Length(min = "2") */ private $city; } @@ -725,15 +731,17 @@ user registers and when a user updates his/her contact information later: registration - - + + - 7 + + + @@ -745,7 +753,7 @@ user registers and when a user updates his/her contact information later: use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\Length; class User { @@ -758,12 +766,14 @@ user registers and when a user updates his/her contact information later: $metadata->addPropertyConstraint('password', new NotBlank(array( 'groups' => array('registration') ))); - $metadata->addPropertyConstraint('password', new MinLength(array( - 'limit' => 7, + $metadata->addPropertyConstraint('password', new Length(array( + 'min' => 7, 'groups' => array('registration') ))); - $metadata->addPropertyConstraint('city', new MinLength(3)); + $metadata->addPropertyConstraint( + 'city', + Length(array("min" => 3))); } } diff --git a/reference/constraints/All.rst b/reference/constraints/All.rst index 57bddc7bde6..b7941006504 100644 --- a/reference/constraints/All.rst +++ b/reference/constraints/All.rst @@ -30,7 +30,8 @@ entry in that array: favoriteColors: - All: - NotBlank: ~ - - MinLength: 5 + - Length: + min: 5 .. code-block:: php-annotations @@ -44,7 +45,7 @@ entry in that array: /** * @Assert\All({ * @Assert\NotBlank - * @Assert\MinLength(5), + * @Assert\Length(min = "5"), * }) */ protected $favoriteColors = array(); diff --git a/reference/constraints/Valid.rst b/reference/constraints/Valid.rst index d634d527aef..4fc91780369 100644 --- a/reference/constraints/Valid.rst +++ b/reference/constraints/Valid.rst @@ -56,7 +56,8 @@ an ``Address`` instance in the ``$address`` property. properties: firstName: - NotBlank: ~ - - MinLength: 4 + - Length: + min: 4 lastName: - NotBlank: ~ @@ -76,7 +77,9 @@ an ``Address`` instance in the ``$address`` property. - 4 + + + @@ -107,7 +110,7 @@ an ``Address`` instance in the ``$address`` property. { /** * @Assert\NotBlank - * @Assert\MinLength(4) + * @Assert\Length(min = "4") */ protected $firstName; @@ -143,7 +146,7 @@ an ``Address`` instance in the ``$address`` property. // src/Acme/HelloBundle/Author.php use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MinLength; + use Symfony\Component\Validator\Constraints\Length; class Author { @@ -156,7 +159,7 @@ an ``Address`` instance in the ``$address`` property. public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('firstName', new NotBlank()); - $metadata->addPropertyConstraint('firstName', new MinLength(4)); + $metadata->addPropertyConstraint('firstName', new Length(array("min" => 4))); $metadata->addPropertyConstraint('lastName', new NotBlank()); } } From c588f8e872a7c38a705015860e3a2fe792ed1dcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 29 Oct 2012 11:13:43 +0100 Subject: [PATCH 2/3] replace maxlength constraints with length equivalents Conflicts: reference/constraints/Collection.rst --- book/forms.rst | 2 +- reference/constraints/Collection.rst | 24 +++++++++++++----------- reference/constraints/Valid.rst | 15 ++++++++++----- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 452aefeb846..01f32af631d 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -563,7 +563,7 @@ the correct values of a number of field options. When these options are set, the field will be rendered with special HTML attributes that provide for HTML5 client-side validation. However, it - doesn't generate the equivalent server-side constraints (e.g. ``Assert\MaxLength``). + doesn't generate the equivalent server-side constraints (e.g. ``Assert\Length``). And though you'll need to manually add your server-side validation, these field type options can then be guessed from that information. diff --git a/reference/constraints/Collection.rst b/reference/constraints/Collection.rst index 8013166b7c7..d72bb5661cc 100644 --- a/reference/constraints/Collection.rst +++ b/reference/constraints/Collection.rst @@ -60,9 +60,9 @@ blank but is no longer than 100 characters in length, you would do the following personal_email: Email short_bio: - NotBlank - - MaxLength: - limit: 100 - message: Your short bio is too long! + - Length: + max: 100 + maxMessage: Your short bio is too long! allowMissingFields: true .. code-block:: php-annotations @@ -78,9 +78,9 @@ blank but is no longer than 100 characters in length, you would do the following * "personal_email" = @Assert\Email, * "short_bio" = { * @Assert\NotBlank(), - * @Assert\MaxLength( - * limit = 100, - * message = "Your bio is too long!" + * @Assert\Length( + * max = 100, + * maxMessage = "Your bio is too long!" * ) * } * }, @@ -105,9 +105,9 @@ blank but is no longer than 100 characters in length, you would do the following - - - + + + @@ -122,7 +122,7 @@ blank but is no longer than 100 characters in length, you would do the following use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\Email; - use Symfony\Component\Validator\Constraints\MaxLength; + use Symfony\Component\Validator\Constraints\Length; class Author { @@ -133,7 +133,9 @@ blank but is no longer than 100 characters in length, you would do the following $metadata->addPropertyConstraint('profileData', new Collection(array( 'fields' => array( 'personal_email' => new Email(), - 'lastName' => array(new NotBlank(), new MaxLength(100)), + 'lastName' => array( + new NotBlank(), + new Length(array("max" => 100)), ), 'allowMissingFields' => true, ))); diff --git a/reference/constraints/Valid.rst b/reference/constraints/Valid.rst index 4fc91780369..7b0268a92c8 100644 --- a/reference/constraints/Valid.rst +++ b/reference/constraints/Valid.rst @@ -50,7 +50,8 @@ an ``Address`` instance in the ``$address`` property. - NotBlank: ~ zipCode: - NotBlank: ~ - - MaxLength: 5 + - Length: + max: 5 Acme\HelloBundle\Author: properties: @@ -70,7 +71,9 @@ an ``Address`` instance in the ``$address`` property. - 5 + + + @@ -100,7 +103,7 @@ an ``Address`` instance in the ``$address`` property. /** * @Assert\NotBlank - * @Assert\MaxLength(5) + * @Assert\MaxLength(max = "5") */ protected $zipCode; } @@ -127,7 +130,7 @@ an ``Address`` instance in the ``$address`` property. // src/Acme/HelloBundle/Address.php use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Constraints\NotBlank; - use Symfony\Component\Validator\Constraints\MaxLength; + use Symfony\Component\Validator\Constraints\Length; class Address { @@ -139,7 +142,9 @@ an ``Address`` instance in the ``$address`` property. { $metadata->addPropertyConstraint('street', new NotBlank()); $metadata->addPropertyConstraint('zipCode', new NotBlank()); - $metadata->addPropertyConstraint('zipCode', new MaxLength(5)); + $metadata->addPropertyConstraint( + 'zipCode', + new Length(array("max" => 5))); } } From b7b0ef03c420978c8d0b83ce8a1de1f60b9d9346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 29 Oct 2012 12:01:55 +0100 Subject: [PATCH 3/3] max_length can also be guessed from Length --- book/forms.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/book/forms.rst b/book/forms.rst index 01f32af631d..7ae29fddc9b 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -573,8 +573,9 @@ the correct values of a number of field options. validation will automatically match your validation rules. * ``max_length``: If the field is some sort of text field, then the ``max_length`` - option can be guessed from the validation constraints (if ``MaxLength`` or ``Max`` - is used) or from the Doctrine metadata (via the field's length). + option can be guessed from the validation constraints (if ``Length`` or + ``MaxLength`` or ``Max`` is used) or from the Doctrine metadata (via the + field's length). .. note::