diff --git a/components/validator.rst b/components/validator.rst index 775be555513..3081a5bc7e0 100644 --- a/components/validator.rst +++ b/components/validator.rst @@ -46,6 +46,8 @@ characters long:: } } +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,