diff --git a/Constraints/Validate.php b/Constraints/Validate.php new file mode 100644 index 0000000..b03f9cd --- /dev/null +++ b/Constraints/Validate.php @@ -0,0 +1,20 @@ + + */ +class Validate extends Constraint +{ + /** + * @var array Embedded groups + */ + public $embeddedGroups; +} diff --git a/Constraints/ValidateValidator.php b/Constraints/ValidateValidator.php new file mode 100644 index 0000000..3ea5cef --- /dev/null +++ b/Constraints/ValidateValidator.php @@ -0,0 +1,45 @@ + + */ +class ValidateValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$constraint instanceof Validate) { + throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Validate'); + } + + if (null === $value) { + return; + } + + if (!is_object($value) && !is_array($value)) { + throw new UnexpectedTypeException($value, 'object'); + } + + // Validate the embedded object(s) + $validator = $this->context->getValidator(); + $violations = $validator->validate($value, null, $constraint->embeddedGroups); + + // Add violations to the calling context + foreach ($violations as $violation) { + $this->context->buildViolation($violation->getMessage()) + ->atPath($violation->getPropertyPath()) + ->setCode($violation->getCode()) + ->addViolation(); + } + } +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e28d6eb --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2016 Fluoresce Ltd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..44e6c0e --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +Validate Embedded +================= + +This library provides the `Validate` constraint for the +[Symfony Validator Component](https://github.com/symfony/validator). + +This constraint behaves similar to the built-in `Valid` constraint, but respects +the `groups` option. Additionally, it adds an option to specify the validation +groups to target on the embedded object(s). + +## Installation + +Open a command console, enter your project directory and execute the following +command to download the latest stable version of this bundle: + +```bash +$ composer require fluoresce/validate-embedded +``` + +This command requires you to have Composer installed globally, as explained in +the [installation chapter](https://getcomposer.org/doc/00-intro.md) of the +Composer documentation. + +## Documentation + +_The following examples assume you are using the full Symfony framework, though +this package can be used in any project that has the Symfony Validator Component +available_ + +### Basic Usage + +This example shows an `Author` which has a collection of `Book`s. When +validation is run for `group1` on `Author`, it will cascade to all embedded +`Book` instances. + +The behaviour here is slightly different to the standard `Valid` constraint, as +validation of the `Book` instances is run with the default group. + +```php +=5.5.9", + "symfony/validator": "~2.8|~3.0" + }, + "minimum-stability": "stable", + "autoload": { + "psr-4": { + "Fluoresce\\ValidateEmbedded\\": "" + } + } +}