Permalink
Browse files

merged branch ricardclau/rename_choice_to_oneof (PR #6360)

This PR was merged into the master branch.

Commits
-------

9071676 create oneof constraint and add deprecation messages in choice, also make choice extend new oneOf constraint to avoid duplicate code

Discussion
----------

[Validator] Rename "Choice" to "OneOf"

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #6324
Todo:
License of the code: MIT
Documentation PR: symfony/symfony-docs#2025

---------------------------------------------------------------------------

by bschussek at 2012-12-14T18:20:56Z

Can you let `Choice` and `ChoiceValidator` extend the renamed classes? Then we avoid the code duplication. You can also remove `ChoiceValidatorTest` then.

---------------------------------------------------------------------------

by ricardclau at 2012-12-14T18:26:58Z

For sure! BTW there seems to be a problem with SF2 build when testing BR currency :S maybe it is the new ICU version supported, I guess you are aware of this but just in case!

---------------------------------------------------------------------------

by stof at 2012-12-14T21:14:04Z

the ``Choice`` constraint should trigger a ``E_USER_DEPRECATED`` in its constructor (and calling the parent constructor) as we agreed on this. The PR adding these calls everywhere is not merged yet, but it would avoid missing it when merging the PRs separately

---------------------------------------------------------------------------

by bschussek at 2012-12-14T21:19:45Z

Things missing:

* add a deprecation note to the Validator's CHANGELOG
* add a block in UPGRADE-2.2 under the Validator's "Deprecations" section that explains how to upgrade with a before/after sample
* add `trigger_error()` calls to the constructors of `Choice` and `ChoiceValidator`

See #6180 for more information about how to call `trigger_error()`.

---------------------------------------------------------------------------

by ricardclau at 2012-12-14T22:45:55Z

Sorry I was not aware of the trigger_error() agreement, absolutely +1 on it.

I hope I understood everything properly, if there is something else or some other errors please tell me!

---------------------------------------------------------------------------

by bschussek at 2012-12-15T08:46:51Z

The entry in the CHANGELOG is still missing.

---------------------------------------------------------------------------

by ricardclau at 2012-12-15T09:36:28Z

By the CHANGELOG you mean what I just added right? Hope now it is ok!

---------------------------------------------------------------------------

by bschussek at 2012-12-15T09:45:16Z

Awesome! ๐Ÿ‘
  • Loading branch information...
2 parents e3cc337 + 9071676 commit 1de60c902c66c01c9c8c24935cd9efe90b6333e5 @fabpot fabpot committed Dec 15, 2012
View
@@ -319,3 +319,21 @@
// ...
}
```
+
+ * The constraint `Choice` was deprecated and will be removed in Symfony 2.3.
+ You should use the new constraint `OneOf` instead.
+
+ Before:
+
+ ```
+ /** @Assert\Choice(choices = {"male", "female"}, message = "Choose a valid gender.") */
+ protected $gender;
+ ```
+
+ After:
+
+ ```
+ /** @Assert\OneOf(choices = {"male", "female"}, message = "Choose a valid gender.") */
+ protected $gender;
+ ```
+
@@ -28,6 +28,7 @@ CHANGELOG
As of Symfony 2.3, this method will be typed against `MetadataFactoryInterface` instead.
* [BC BREAK] the switches `traverse` and `deep` in the `Valid` constraint and in `GraphWalker::walkReference`
are ignored for arrays now. Arrays are always traversed recursively.
+ * deprecated `Choice` constraint in favor of the new `OneOf` constraint.
2.1.0
-----
@@ -11,31 +11,19 @@
namespace Symfony\Component\Validator\Constraints;
-use Symfony\Component\Validator\Constraint;
-
/**
* @Annotation
*
* @api
+ *
+ * @deprecated Deprecated since version 2.2, to be removed in 2.3. Use
+ * {@link OneOf} instead.
*/
-class Choice extends Constraint
+class Choice extends OneOf
{
- public $choices;
- public $callback;
- public $multiple = false;
- public $strict = false;
- public $min = null;
- public $max = null;
- public $message = 'The value you selected is not a valid choice.';
- public $multipleMessage = 'One or more of the given values is invalid.';
- public $minMessage = 'You must select at least {{ limit }} choices.';
- public $maxMessage = 'You must select at most {{ limit }} choices.';
-
- /**
- * {@inheritDoc}
- */
- public function getDefaultOption()
+ public function __construct($options = null)
{
- return 'choices';
+ trigger_error('Choice constraint is deprecated since version 2.2 and will be removed in 2.3. Use OneOf instead', E_USER_DEPRECATED);
+ parent::__construct($options);
}
}
@@ -11,11 +11,6 @@
namespace Symfony\Component\Validator\Constraints;
-use Symfony\Component\Validator\Constraint;
-use Symfony\Component\Validator\ConstraintValidator;
-use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
-use Symfony\Component\Validator\Exception\UnexpectedTypeException;
-
/**
* ChoiceValidator validates that the value is one of the expected values.
*
@@ -24,60 +19,15 @@
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
+ *
+ * @deprecated Deprecated since version 2.2, to be removed in 2.3. Use
+ * {@link OneOfValidator} instead.
*/
-class ChoiceValidator extends ConstraintValidator
+class ChoiceValidator extends OneOfValidator
{
- /**
- * {@inheritDoc}
- */
- public function validate($value, Constraint $constraint)
+ public function __construct($options = null)
{
- if (!$constraint->choices && !$constraint->callback) {
- throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
- }
-
- if (null === $value) {
- return;
- }
-
- if ($constraint->multiple && !is_array($value)) {
- throw new UnexpectedTypeException($value, 'array');
- }
-
- if ($constraint->callback) {
- if (is_callable(array($this->context->getCurrentClass(), $constraint->callback))) {
- $choices = call_user_func(array($this->context->getCurrentClass(), $constraint->callback));
- } elseif (is_callable($constraint->callback)) {
- $choices = call_user_func($constraint->callback);
- } else {
- throw new ConstraintDefinitionException('The Choice constraint expects a valid callback');
- }
- } else {
- $choices = $constraint->choices;
- }
-
- if ($constraint->multiple) {
- foreach ($value as $_value) {
- if (!in_array($_value, $choices, $constraint->strict)) {
- $this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
- }
- }
-
- $count = count($value);
-
- if ($constraint->min !== null && $count < $constraint->min) {
- $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
-
- return;
- }
-
- if ($constraint->max !== null && $count > $constraint->max) {
- $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
-
- return;
- }
- } elseif (!in_array($value, $choices, $constraint->strict)) {
- $this->context->addViolation($constraint->message, array('{{ value }}' => $value));
- }
+ trigger_error('ChoiceValidator is deprecated since version 2.2 and will be removed in 2.3. Use OneOfValidator instead', E_USER_DEPRECATED);
+ parent::__construct($options);
}
}
@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+
+/**
+ * @Annotation
+ *
+ * @api
+ */
+class OneOf extends Constraint
+{
+ public $choices;
+ public $callback;
+ public $multiple = false;
+ public $strict = false;
+ public $min = null;
+ public $max = null;
+ public $message = 'The value you selected is not a valid choice.';
+ public $multipleMessage = 'One or more of the given values is invalid.';
+ public $minMessage = 'You must select at least {{ limit }} choices.';
+ public $maxMessage = 'You must select at most {{ limit }} choices.';
+
+ /**
+ * {@inheritDoc}
+ */
+ public function getDefaultOption()
+ {
+ return 'choices';
+ }
+}
@@ -0,0 +1,83 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Validator\Constraints;
+
+use Symfony\Component\Validator\Constraint;
+use Symfony\Component\Validator\ConstraintValidator;
+use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
+use Symfony\Component\Validator\Exception\UnexpectedTypeException;
+
+/**
+ * ChoiceValidator validates that the value is one of the expected values.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ * @author Florian Eckerstorfer <florian@eckerstorfer.org>
+ * @author Bernhard Schussek <bschussek@gmail.com>
+ *
+ * @api
+ */
+class OneOfValidator extends ConstraintValidator
+{
+ /**
+ * {@inheritDoc}
+ */
+ public function validate($value, Constraint $constraint)
+ {
+ if (!$constraint->choices && !$constraint->callback) {
+ throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
+ }
+
+ if (null === $value) {
+ return;
+ }
+
+ if ($constraint->multiple && !is_array($value)) {
+ throw new UnexpectedTypeException($value, 'array');
+ }
+
+ if ($constraint->callback) {
+ if (is_callable(array($this->context->getCurrentClass(), $constraint->callback))) {
+ $choices = call_user_func(array($this->context->getCurrentClass(), $constraint->callback));
+ } elseif (is_callable($constraint->callback)) {
+ $choices = call_user_func($constraint->callback);
+ } else {
+ throw new ConstraintDefinitionException('The OneOf constraint expects a valid callback');
+ }
+ } else {
+ $choices = $constraint->choices;
+ }
+
+ if ($constraint->multiple) {
+ foreach ($value as $_value) {
+ if (!in_array($_value, $choices, $constraint->strict)) {
+ $this->context->addViolation($constraint->multipleMessage, array('{{ value }}' => $_value));
+ }
+ }
+
+ $count = count($value);
+
+ if ($constraint->min !== null && $count < $constraint->min) {
+ $this->context->addViolation($constraint->minMessage, array('{{ limit }}' => $constraint->min), null, (int) $constraint->min);
+
+ return;
+ }
+
+ if ($constraint->max !== null && $count > $constraint->max) {
+ $this->context->addViolation($constraint->maxMessage, array('{{ limit }}' => $constraint->max), null, (int) $constraint->max);
+
+ return;
+ }
+ } elseif (!in_array($value, $choices, $constraint->strict)) {
+ $this->context->addViolation($constraint->message, array('{{ value }}' => $value));
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit 1de60c9

Please sign in to comment.