From c8b6b457bc9da550a3200683176d2121065b019c Mon Sep 17 00:00:00 2001 From: Alexandru Bucur Date: Fri, 25 Nov 2016 10:39:15 +0200 Subject: [PATCH 1/3] missing constraint example from the old readme --- components/validator.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/components/validator.rst b/components/validator.rst index 775be555513..29a6abcaa1f 100644 --- a/components/validator.rst +++ b/components/validator.rst @@ -45,6 +45,31 @@ characters long:: echo $violation->getMessage().'
'; } } + +Validation of arrays is possible using the `Collection` constraint:: + + + use Symfony\Component\Validator\Validation; + use Symfony\Component\Validator\Constraints as Assert; + + $validator = Validation::createValidator(); + + $constraint = new Assert\Collection(array( + 'name' => new Assert\Collection(array( + 'first_name' => new Assert\Length(array('min' => 101)), + 'last_name' => new Assert\Length(array('min' => 1)), + )), + 'email' => new Assert\Email(), + 'simple' => new Assert\Length(array('min' => 102)), + 'gender' => new Assert\Choice(array(3, 4)), + 'file' => new Assert\File(), + 'password' => new Assert\Length(array('min' => 60)), + )); + + $violations = $validator->validateValue($input, $constraint); + + +Again, the validator returns the list of violations. Retrieving a Validator Instance ------------------------------- From 64f5ad950dff5ba807d78f1fcba79d44ebfff178 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 25 Nov 2016 12:37:30 +0100 Subject: [PATCH 2/3] Fixed some syntax issues --- components/validator.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/components/validator.rst b/components/validator.rst index 29a6abcaa1f..b10b4e69b28 100644 --- a/components/validator.rst +++ b/components/validator.rst @@ -46,8 +46,7 @@ characters long:: } } -Validation of arrays is possible using the `Collection` constraint:: - +Validation of arrays is possible using the ``Collection`` constraint:: use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Constraints as Assert; @@ -68,7 +67,6 @@ Validation of arrays is possible using the `Collection` constraint:: $violations = $validator->validateValue($input, $constraint); - Again, the validator returns the list of violations. Retrieving a Validator Instance From 06f67b40e7c410db4523363b48ad6e4c68f997d4 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 15 Apr 2017 17:25:56 +0200 Subject: [PATCH 3/3] Moved array validation to the Raw values sub-guide --- components/validator.rst | 23 +---------------------- validation/raw_values.rst | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/components/validator.rst b/components/validator.rst index b10b4e69b28..3081a5bc7e0 100644 --- a/components/validator.rst +++ b/components/validator.rst @@ -45,29 +45,8 @@ characters long:: echo $violation->getMessage().'
'; } } - -Validation of arrays is possible using the ``Collection`` constraint:: - use Symfony\Component\Validator\Validation; - use Symfony\Component\Validator\Constraints as Assert; - - $validator = Validation::createValidator(); - - $constraint = new Assert\Collection(array( - 'name' => new Assert\Collection(array( - 'first_name' => new Assert\Length(array('min' => 101)), - 'last_name' => new Assert\Length(array('min' => 1)), - )), - 'email' => new Assert\Email(), - 'simple' => new Assert\Length(array('min' => 102)), - 'gender' => new Assert\Choice(array(3, 4)), - 'file' => new Assert\File(), - 'password' => new Assert\Length(array('min' => 60)), - )); - - $violations = $validator->validateValue($input, $constraint); - -Again, the validator returns the list of violations. +The validator returns the list of violations. Retrieving a Validator Instance ------------------------------- diff --git a/validation/raw_values.rst b/validation/raw_values.rst index 56e4568979e..df340fe3e79 100644 --- a/validation/raw_values.rst +++ b/validation/raw_values.rst @@ -46,12 +46,34 @@ looks like this:: // ... } -By calling ``validate`` on the validator, you can pass in a raw value and +By calling ``validate()`` on the validator, you can pass in a raw value and the constraint object that you want to validate that value against. A full list of the available constraints - as well as the full class name for each constraint - is available in the :doc:`constraints reference ` section. +Validation of arrays is possible using the ``Collection`` constraint:: + + use Symfony\Component\Validator\Validation; + use Symfony\Component\Validator\Constraints as Assert; + + $validator = Validation::createValidator(); + + $constraint = new Assert\Collection(array( + // the keys correspond to the keys in the input array + 'name' => new Assert\Collection(array( + 'first_name' => new Assert\Length(array('min' => 101)), + 'last_name' => new Assert\Length(array('min' => 1)), + )), + 'email' => new Assert\Email(), + 'simple' => new Assert\Length(array('min' => 102)), + 'gender' => new Assert\Choice(array(3, 4)), + 'file' => new Assert\File(), + 'password' => new Assert\Length(array('min' => 60)), + )); + + $violations = $validator->validateValue($input, $constraint); + The ``validate()`` method returns a :class:`Symfony\\Component\\Validator\\ConstraintViolationList` object, which acts just like an array of errors. Each error in the collection is a :class:`Symfony\\Component\\Validator\\ConstraintViolation` object,