diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index 2f10889..3c4843e 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -12,9 +12,8 @@ use Drupal\Core\Plugin\Discovery\HookDiscovery; use Drupal\Core\TypedData\Validation\MetadataFactory; use Drupal\Core\Validation\ConstraintManager; -use Drupal\Core\Validation\Validator; -use Symfony\Component\Validator\ConstraintValidatorFactory; use Symfony\Component\Validator\ValidatorInterface; +use Symfony\Component\Validator\Validation; /** * Manages data type plugins. @@ -146,7 +145,9 @@ public function setValidator(ValidatorInterface $validator) { */ public function getValidator() { if (!isset($this->validator)) { - $this->validator = new Validator(new MetadataFactory(), new ConstraintValidatorFactory()); + $this->validator = Validation::createValidatorBuilder() + ->setMetadataFactory(new MetadataFactory()) + ->getValidator(); } return $this->validator; } @@ -184,7 +185,7 @@ public function getValidationConstraintManager() { * The name or plugin id of the constraint. * @param mixed $options * The options to pass to the constraint class. Required and supported - * options depend on the class. + * options depend on the constraint class. * * @return \Symfony\Component\Validator\Constraint * A validation constraint plugin. @@ -196,11 +197,6 @@ public function createValidationConstraint($name, $options) { // 'value' key also. $options = array('value' => $options); } - // Support specifying constraint via the 'constraint' and 'value' keys. - elseif (isset($options['value']) && isset($options['constraint']) && count($options) == 2) { - $name = $options['constraint']; - unset($options['constraint']); - } return $this->getValidationConstraintManager()->createInstance($name, $options); } @@ -211,25 +207,18 @@ public function createValidationConstraint($name, $options) { * of the type's plugin definition, or constraints defined below the data * definition's constraint' key are taken into account. * - * Constraints care defined via an array, having constraint plugin IDs as key + * Constraints are defined via an array, having constraint plugin IDs as key * and constraint options as values, e.g. * @code * $constraints = array( - * 'Min' => array('limit' => 5), - * ); - * @endcode - * - * Alternatively, the constraint can be defined using an arbitrary key if - * the constraint plugin ID is passed as part below a 'constraint' sub-key and - * the options below a 'value' sub-key, e.g. - * @code - * $constraints = array( - * array('constraint' => 'Min', 'value' => array('limit' => 5)), + * 'Min' => 5, + * 'Range' => array('min' => 5, 'max' => 10), + * 'NotBlank' => array(), * ); * @endcode - * The latter notation allows specifying constraints of the same plugin - * multiple times, whereas the former is commonly used and easier to - * read. + * Options have to be specified using another array if the constraint has more + * than one or zero options. If it has exactly one option, the value should be + * specified without nesting it into another array. * * Note that the specified constraints must be compatible with the data type, * e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints diff --git a/core/lib/Drupal/Core/Validation/ConstraintViolation.php b/core/lib/Drupal/Core/Validation/ConstraintViolation.php deleted file mode 100644 index ed59545..0000000 --- a/core/lib/Drupal/Core/Validation/ConstraintViolation.php +++ /dev/null @@ -1,31 +0,0 @@ -getMessageParameters(); - - foreach ($parameters as $i => $parameter) { - if (is_array($parameter)) { - $parameters[$i] = 'Array'; - } - } - // t() will escape Symfony message replacements like '{ limit }' as - // placeholder. See format_string(). - return t($this->getMessageTemplate(), $parameters); - } -} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Validation/ExecutionContext.php b/core/lib/Drupal/Core/Validation/ExecutionContext.php deleted file mode 100644 index 360cbda..0000000 --- a/core/lib/Drupal/Core/Validation/ExecutionContext.php +++ /dev/null @@ -1,31 +0,0 @@ -globalContext->getViolations()->add(new ConstraintViolation( - $message, - $params, - $this->globalContext->getRoot(), - $this->getPropertyPath(), - // check using func_num_args() to allow passing NULL values - func_num_args() >= 3 ? $invalidValue : $this->getValue(), - $pluralization, - $code - )); - } -} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Validation/SymfonyDiscoveryDecorator.php b/core/lib/Drupal/Core/Validation/SymfonyDiscoveryDecorator.php index 2a40aa8..047086a 100644 --- a/core/lib/Drupal/Core/Validation/SymfonyDiscoveryDecorator.php +++ b/core/lib/Drupal/Core/Validation/SymfonyDiscoveryDecorator.php @@ -59,6 +59,11 @@ public function getDefinitions() { 'class' => '\Symfony\Component\Validator\Constraints\Max', 'type' => array('integer', 'float'), ); + $definitions['Range'] = array( + 'label' => t('Range'), + 'class' => '\Symfony\Component\Validator\Constraints\Range', + 'type' => array('integer', 'float'), + ); $definitions['MinLength'] = array( 'label' => t('Minimum length'), 'class' => '\Symfony\Component\Validator\Constraints\MinLength', diff --git a/core/lib/Drupal/Core/Validation/ValidationVisitor.php b/core/lib/Drupal/Core/Validation/ValidationVisitor.php deleted file mode 100644 index ffef0cf..0000000 --- a/core/lib/Drupal/Core/Validation/ValidationVisitor.php +++ /dev/null @@ -1,31 +0,0 @@ -validateValue($value, $metadata->findConstraints($group)); - } -} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Validation/Validator.php b/core/lib/Drupal/Core/Validation/Validator.php deleted file mode 100644 index 84e9db3..0000000 --- a/core/lib/Drupal/Core/Validation/Validator.php +++ /dev/null @@ -1,37 +0,0 @@ -metadataFactory, $this->validatorFactory, $this->objectInitializers); - } -} \ No newline at end of file diff --git a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php index 2098a2c..89d9927 100644 --- a/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/TypedData/TypedDataTest.php @@ -191,7 +191,7 @@ public function testTypedDataValidation() { $definition = array( 'type' => 'integer', 'constraints' => array( - 'Min' => array('limit' => 5), + 'Min' => 5, ), ); $violations = $this->typedData->create($definition, 10)->validate(); @@ -202,7 +202,11 @@ public function testTypedDataValidation() { $this->assertEqual($violations->count(), 1); $message = t('This value should be {{ limit }} or more.', array('{{ limit }}' => 5)); - $this->assertEqual($violations[0]->getMessage(), $message, 'Translated violation message retrieved.'); + // Right now there is no support for getting translated messages, so test + // doing it manually for now. + // @todo: Replace this to use the API for getting translations. + $translation = t($violations[0]->getMessageTemplate(), $violations[0]->getMessageParameters()); + $this->assertEqual($translation, $message, 'Translated violation message retrieved.'); $this->assertEqual($violations[0]->getPropertyPath(), ''); $this->assertEqual($violations[0]->getPropertyPath(), ''); $this->assertIdentical($violations[0]->getRoot(), $integer, 'Root object returned.'); @@ -211,7 +215,7 @@ public function testTypedDataValidation() { $definition = array( 'type' => 'integer', 'constraints' => array( - 'Min' => array('limit' => 5), + 'Min' => 5, 'Null' => array(), ), ); @@ -256,17 +260,6 @@ public function testTypedDataValidation() { $violations = $field_item->validate(); $this->assertEqual($violations->count(), 0); - // Test specifying the same constraint plugin multiple times. - $definition = array( - 'type' => 'integer', - 'constraints' => array( - array('constraint' => 'Min', 'value' => array('limit' => 5)), - array('constraint' => 'Min', 'value' => array('limit' => 10)), - ), - ); - $violations = $this->typedData->create($definition, 1)->validate(); - $this->assertEqual($violations->count(), 2, 'Both constraints are violated.'); - // Test getting constraint definitions by type. $definitions = $this->typedData->getValidationConstraintManager()->getDefinitionsByType('entity'); $this->assertTrue(isset($definitions['EntityType']), 'Constraint plugin found for type entity.');