|
35 | 35 | ### Form
|
36 | 36 |
|
37 | 37 | * The PasswordType is now not trimmed by default.
|
| 38 | + |
| 39 | +### Validator |
| 40 | + |
| 41 | + * Interfaces were created for created for the classes `ConstraintViolation`, |
| 42 | + `ConstraintViolationList`, `GlobalExecutionContext` and `ExecutionContext`. |
| 43 | + If you type hinted against any of these classes, you are recommended to |
| 44 | + type hint against their interfaces now. |
| 45 | + |
| 46 | + Before: |
| 47 | + |
| 48 | + ``` |
| 49 | + use Symfony\Component\Validator\ExecutionContext; |
| 50 | +
|
| 51 | + public function validateCustomLogic(ExecutionContext $context) |
| 52 | + ``` |
| 53 | + |
| 54 | + After: |
| 55 | + |
| 56 | + ``` |
| 57 | + use Symfony\Component\Validator\ExecutionContext; |
| 58 | +
|
| 59 | + public function validateCustomLogic(ExecutionContextInterface $context) |
| 60 | + ``` |
| 61 | + |
| 62 | + For all implementations of `ConstraintValidatorInterface`, this change is |
| 63 | + mandatory for the `initialize` method: |
| 64 | + |
| 65 | + Before: |
| 66 | + |
| 67 | + ``` |
| 68 | + use Symfony\Component\Validator\ConstraintValidatorInterface; |
| 69 | + use Symfony\Component\Validator\ExecutionContext; |
| 70 | +
|
| 71 | + class MyValidator implements ConstraintValidatorInterface |
| 72 | + { |
| 73 | + public function initialize(ExecutionContext $context) |
| 74 | + { |
| 75 | + // ... |
| 76 | + } |
| 77 | + } |
| 78 | + ``` |
| 79 | + |
| 80 | + After: |
| 81 | + |
| 82 | + ``` |
| 83 | + use Symfony\Component\Validator\ConstraintValidatorInterface; |
| 84 | + use Symfony\Component\Validator\ExecutionContextInterface; |
| 85 | +
|
| 86 | + class MyValidator implements ConstraintValidatorInterface |
| 87 | + { |
| 88 | + public function initialize(ExecutionContextInterface $context) |
| 89 | + { |
| 90 | + // ... |
| 91 | + } |
| 92 | + } |
| 93 | + ``` |
| 94 | + |
| 95 | +#### Deprecations |
| 96 | + |
| 97 | + * The interface `ClassMetadataFactoryInterface` was deprecated and will be |
| 98 | + removed in Symfony 2.3. You should implement `MetadataFactoryInterface` |
| 99 | + instead, which changes the name of the method `getClassMetadata` to |
| 100 | + `getMetadataFor` and accepts arbitrary values (e.g. class names, objects, |
| 101 | + numbers etc.). In your implementation, you should throw a |
| 102 | + `NoSuchMetadataException` if you don't support metadata for the given value. |
| 103 | + |
| 104 | + Before: |
| 105 | + |
| 106 | + ``` |
| 107 | + use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface; |
| 108 | +
|
| 109 | + class MyMetadataFactory implements ClassMetadataFactoryInterface |
| 110 | + { |
| 111 | + public function getClassMetadata($class) |
| 112 | + { |
| 113 | + // ... |
| 114 | + } |
| 115 | + } |
| 116 | + ``` |
| 117 | + |
| 118 | + After: |
| 119 | + |
| 120 | + ``` |
| 121 | + use Symfony\Component\Validator\MetadataFactoryInterface; |
| 122 | + use Symfony\Component\Validator\Exception\NoSuchMetadataException; |
| 123 | +
|
| 124 | + class MyMetadataFactory implements MetadataFactoryInterface |
| 125 | + { |
| 126 | + public function getMetadataFor($value) |
| 127 | + { |
| 128 | + if (is_object($value)) { |
| 129 | + $value = get_class($value); |
| 130 | + } |
| 131 | +
|
| 132 | + if (!is_string($value) || (!class_exists($value) && !interface_exists($value))) { |
| 133 | + throw new NoSuchMetadataException(...); |
| 134 | + } |
| 135 | +
|
| 136 | + // ... |
| 137 | + } |
| 138 | + } |
| 139 | + ``` |
| 140 | + |
| 141 | + The return value of `ValidatorInterface::getMetadataFactory()` was also |
| 142 | + changed to return `MetadataFactoryInterface`. Make sure to replace calls to |
| 143 | + `getClassMetadata` by `getMetadataFor` on the return value of this method. |
| 144 | + |
| 145 | + Before: |
| 146 | + |
| 147 | + ``` |
| 148 | + $metadataFactory = $validator->getMetadataFactory(); |
| 149 | + $metadata = $metadataFactory->getClassMetadata('Vendor\MyClass'); |
| 150 | + ``` |
| 151 | + |
| 152 | + After: |
| 153 | + |
| 154 | + ``` |
| 155 | + $metadataFactory = $validator->getMetadataFactory(); |
| 156 | + $metadata = $metadataFactory->getMetadataFor('Vendor\MyClass'); |
| 157 | + ``` |
| 158 | + |
| 159 | + * The class `GraphWalker` and the accessor `ExecutionContext::getGraphWalker()` |
| 160 | + were deprecated and will be removed in Symfony 2.3. You should use the |
| 161 | + methods `ExecutionContextInterface::validate()` and |
| 162 | + `ExecutionContextInterface::validateValue()` instead. |
| 163 | + |
| 164 | + Before: |
| 165 | + |
| 166 | + ``` |
| 167 | + use Symfony\Component\Validator\ExecutionContext; |
| 168 | +
|
| 169 | + public function validateCustomLogic(ExecutionContext $context) |
| 170 | + { |
| 171 | + if (/* ... */) { |
| 172 | + $path = $context->getPropertyPath(); |
| 173 | + $group = $context->getGroup(); |
| 174 | +
|
| 175 | + if (!empty($path)) { |
| 176 | + $path .= '.'; |
| 177 | + } |
| 178 | +
|
| 179 | + $context->getGraphWalker()->walkReference($someObject, $group, $path . 'myProperty', false); |
| 180 | + } |
| 181 | + } |
| 182 | + ``` |
| 183 | + |
| 184 | + After: |
| 185 | + |
| 186 | + ``` |
| 187 | + use Symfony\Component\Validator\ExecutionContextInterface; |
| 188 | +
|
| 189 | + public function validateCustomLogic(ExecutionContextInterface $context) |
| 190 | + { |
| 191 | + if (/* ... */) { |
| 192 | + $context->validate($someObject, 'myProperty'); |
| 193 | + } |
| 194 | + } |
| 195 | + ``` |
| 196 | + |
| 197 | + * The method `ExecutionContext::addViolationAtSubPath()` was deprecated and |
| 198 | + will be removed in Symfony 2.3. You should use `addViolationAt()` instead. |
| 199 | + |
| 200 | + Before: |
| 201 | + |
| 202 | + ``` |
| 203 | + use Symfony\Component\Validator\ExecutionContext; |
| 204 | +
|
| 205 | + public function validateCustomLogic(ExecutionContext $context) |
| 206 | + { |
| 207 | + if (/* ... */) { |
| 208 | + $context->addViolationAtSubPath('myProperty', 'This value is invalid'); |
| 209 | + } |
| 210 | + } |
| 211 | + ``` |
| 212 | + |
| 213 | + After: |
| 214 | + |
| 215 | + ``` |
| 216 | + use Symfony\Component\Validator\ExecutionContextInterface; |
| 217 | +
|
| 218 | + public function validateCustomLogic(ExecutionContextInterface $context) |
| 219 | + { |
| 220 | + if (/* ... */) { |
| 221 | + $context->addViolationAt('myProperty', 'This value is invalid'); |
| 222 | + } |
| 223 | + } |
| 224 | + ``` |
| 225 | + |
| 226 | + * The methods `ExecutionContext::getCurrentClass()`, `ExecutionContext::getCurrentProperty()` |
| 227 | + and `ExecutionContext::getCurrentValue()` were deprecated and will be removed |
| 228 | + in Symfony 2.3. Use the methods `getClassName()`, `getPropertyName()` and |
| 229 | + `getValue()` instead. |
| 230 | + |
| 231 | + Before: |
| 232 | + |
| 233 | + ``` |
| 234 | + use Symfony\Component\Validator\ExecutionContext; |
| 235 | +
|
| 236 | + public function validateCustomLogic(ExecutionContext $context) |
| 237 | + { |
| 238 | + $class = $context->getCurrentClass(); |
| 239 | + $property = $context->getCurrentProperty(); |
| 240 | + $value = $context->getCurrentValue(); |
| 241 | +
|
| 242 | + // ... |
| 243 | + } |
| 244 | + ``` |
| 245 | + |
| 246 | + After: |
| 247 | + |
| 248 | + ``` |
| 249 | + use Symfony\Component\Validator\ExecutionContextInterface; |
| 250 | +
|
| 251 | + public function validateCustomLogic(ExecutionContextInterface $context) |
| 252 | + { |
| 253 | + $class = $context->getClassName(); |
| 254 | + $property = $context->getPropertyName(); |
| 255 | + $value = $context->getValue(); |
| 256 | +
|
| 257 | + // ... |
| 258 | + } |
| 259 | + ``` |
0 commit comments