Permalink
Browse files

[Validator] Added instructions on integrating the latest Validator ch…

…anges to the UPGRADE file
  • Loading branch information...
1 parent ee90986 commit 24c653045df74302ff9d15763c78b5a1922671d5 @webmozart webmozart committed Nov 24, 2012
Showing with 222 additions and 1 deletion.
  1. +222 −0 UPGRADE-2.2.md
  2. +0 −1 src/Symfony/Component/Validator/CHANGELOG.md
View
@@ -35,3 +35,225 @@
### Form
* The PasswordType is now not trimmed by default.
+
+### Validator
+
+ * Interfaces were created for created for the classes `ConstraintViolation`,
@ragtek

ragtek Nov 26, 2012

Contributor

were created for created for the classes

should be

were created for the classes

+ `ConstraintViolationList`, `GlobalExecutionContext` and `ExecutionContext`.
+ If you type hinted against any of these classes, you are recommended to
+ type hint against their interfaces now.
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContext;
+
+ public function validateCustomLogic(ExecutionContext $context)
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContext;
+
+ public function validateCustomLogic(ExecutionContextInterface $context)
+ ```
+
+ For all implementations of `ConstraintValidatorInterface`, this change is
+ mandatory for the `initialize` method:
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\ConstraintValidatorInterface;
+ use Symfony\Component\Validator\ExecutionContext;
+
+ class MyValidator implements ConstraintValidatorInterface
+ {
+ public function initialize(ExecutionContext $context)
+ {
+ // ...
+ }
+ }
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\ConstraintValidatorInterface;
+ use Symfony\Component\Validator\ExecutionContextInterface;
+
+ class MyValidator implements ConstraintValidatorInterface
+ {
+ public function initialize(ExecutionContextInterface $context)
+ {
+ // ...
+ }
+ }
+ ```
+
+#### Deprecations
+
+ * The interface `ClassMetadataFactoryInterface` was deprecated and will be
+ removed in Symfony 2.3. You should implement `MetadataFactoryInterface`
+ instead, which changes the name of the method `getClassMetadata` to
+ `getMetadataFor` and accepts arbitrary values (e.g. class names, objects,
+ numbers etc.). In your implementation, you should throw a
+ `NoSuchMetadataException` if you don't support metadata for the given value.
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
+
+ class MyMetadataFactory implements ClassMetadataFactoryInterface
+ {
+ public function getClassMetadata($class)
+ {
+ // ...
+ }
+ }
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\MetadataFactoryInterface;
+ use Symfony\Component\Validator\Exception\NoSuchMetadataException;
+
+ class MyMetadataFactory implements MetadataFactoryInterface
+ {
+ public function getMetadataFor($value)
+ {
+ if (is_object($value)) {
+ $value = get_class($value);
+ }
+
+ if (!is_string($value) || (!class_exists($value) && !interface_exists($value))) {
+ throw new NoSuchMetadataException(...);
+ }
+
+ // ...
+ }
+ }
+ ```
+
+ The return value of `ValidatorInterface::getMetadataFactory()` was also
+ changed to return `MetadataFactoryInterface`. Make sure to replace calls to
+ `getClassMetadata` by `getMetadataFor` on the return value of this method.
+
+ Before:
+
+ ```
+ $metadataFactory = $validator->getMetadataFactory();
+ $metadata = $metadataFactory->getClassMetadata('Vendor\MyClass');
+ ```
+
+ After:
+
+ ```
+ $metadataFactory = $validator->getMetadataFactory();
+ $metadata = $metadataFactory->getMetadataFor('Vendor\MyClass');
+ ```
+
+ * The class `GraphWalker` and the accessor `ExecutionContext::getGraphWalker()`
+ were deprecated and will be removed in Symfony 2.3. You should use the
+ methods `ExecutionContextInterface::validate()` and
+ `ExecutionContextInterface::validateValue()` instead.
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContext;
+
+ public function validateCustomLogic(ExecutionContext $context)
+ {
+ if (/* ... */) {
+ $path = $context->getPropertyPath();
+ $group = $context->getGroup();
+
+ if (!empty($path)) {
+ $path .= '.';
+ }
+
+ $context->getGraphWalker()->walkReference($someObject, $group, $path . 'myProperty', false);
+ }
+ }
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContextInterface;
+
+ public function validateCustomLogic(ExecutionContextInterface $context)
+ {
+ if (/* ... */) {
+ $context->validate($someObject, 'myProperty');
+ }
+ }
+ ```
+
+ * The method `ExecutionContext::addViolationAtSubPath()` was deprecated and
+ will be removed in Symfony 2.3. You should use `addViolationAt()` instead.
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContext;
+
+ public function validateCustomLogic(ExecutionContext $context)
+ {
+ if (/* ... */) {
+ $context->addViolationAtSubPath('myProperty', 'This value is invalid');
+ }
+ }
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContextInterface;
+
+ public function validateCustomLogic(ExecutionContextInterface $context)
+ {
+ if (/* ... */) {
+ $context->addViolationAt('myProperty', 'This value is invalid');
+ }
+ }
+ ```
+
+ * The methods `ExecutionContext::getCurrentClass()`, `ExecutionContext::getCurrentProperty()`
+ and `ExecutionContext::getCurrentValue()` were deprecated and will be removed
+ in Symfony 2.3. Use the methods `getClassName()`, `getPropertyName()` and
+ `getValue()` instead.
+
+ Before:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContext;
+
+ public function validateCustomLogic(ExecutionContext $context)
+ {
+ $class = $context->getCurrentClass();
+ $property = $context->getCurrentProperty();
+ $value = $context->getCurrentValue();
+
+ // ...
+ }
+ ```
+
+ After:
+
+ ```
+ use Symfony\Component\Validator\ExecutionContextInterface;
+
+ public function validateCustomLogic(ExecutionContextInterface $context)
+ {
+ $class = $context->getClassName();
+ $property = $context->getPropertyName();
+ $value = $context->getValue();
+
+ // ...
+ }
+ ```
@@ -22,7 +22,6 @@ CHANGELOG
* deprecated `ExecutionContext::getCurrentProperty` in favor of `ExecutionContextInterface::getPropertyName`
* deprecated `ExecutionContext::getCurrentValue` in favor of `ExecutionContextInterface::getValue`
* deprecated `ExecutionContext::getGraphWalker` in favor of `ExecutionContextInterface::validate` and `ExecutionContextInterface::validateValue`
- * deprecated `ExecutionContext::getMetadataFactory` in favor of `ExecutionContextInterface::getMetadataFor`
* improved `ValidatorInterface::validateValue` to accept arrays of constraints
* changed `ValidatorInterface::getMetadataFactory` to return a `MetadataFactoryInterface` instead of a `ClassMetadataFactoryInterface`
* removed `ClassMetadataFactoryInterface` type hint from `ValidatorBuilderInterface::setMetadataFactory`.

0 comments on commit 24c6530

Please sign in to comment.