10000 [Validator] Deprecated the Size constraint in favor of MinCount and MaxCount by webmozart · Pull Request #4851 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Deprecated the Size constraint in favor of MinCount and MaxCount #4851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,26 @@
private $recursiveCollection;
```

* The `Size` constraint was deprecated and will be removed in Symfony 2.3. You should
use the constraints `Min` and `Max` instead.

Before:

```
/** @Assert\Size(min = 2, max = 16) */
private $numberOfCpus;
```

After:

```
/**
* @Assert\Min(2)
* @Assert\Max(16)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shoudln't it be MinCount and MaxCount ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah no sorry, the 2.0 Size was not about the size of an array

*/
private $numberOfCpus;
```

### Session

* Flash messages now return an array based on their type. The old method is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,14 @@ public function guessTypeForConstraint(Constraint $constraint)
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Size':
switch ($constraint->type) {
case 'string':
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
case 'collection':
return new TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE);
}
break;

case 'Symfony\Component\Validator\Constraints\Min':
case 'Symfony\Component\Validator\Constraints\Range':
case 'Symfony\Component\Validator\Constraints\Max':
return new TypeGuess('number', array(), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\MinCount':
case 'Symfony\Component\Validator\Constraints\MaxCount':
return new TypeGuess('collection', array(), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Time':
return new TypeGuess('time', array('input'=>'string'), Guess::HIGH_CONFIDENCE);

Expand Down Expand Up @@ -208,12 +203,6 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\MaxLength':
return new ValueGuess($constraint->limit, Guess::HIGH_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Size':
if ('string' === $constraint->type && null !== $constraint->max) {
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
}
break;

case 'Symfony\Component\Validator\Constraints\Type':
if (in_array($constraint->type, array('double', 'float', 'numeric', 'real'))) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
Expand All @@ -223,7 +212,7 @@ public function guessMaxLengthForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\Max':
return new ValueGuess(strlen((string) $constraint->limit), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Range':
case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
}
}
Expand All @@ -241,25 +230,6 @@ public function guessPatternForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\MinLength':
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->limit), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Size':
if ('string' !== $constraint->type) {
return;
}

if ($constraint->min === $constraint->max) {
return new ValueGuess(sprintf('.{%s}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
}

if (null === $constraint->min) {
return new ValueGuess(sprintf('.{0,%s}', (string) $constraint->max), Guess::LOW_CONFIDENCE);
}

if (null === $constraint->max) {
return new ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
}

return new ValueGuess(sprintf('.{%s,%s}', (string) $constraint->min, (string) $constraint->max), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Regex':
$htmlPattern = $constraint->getHtmlPattern();

Expand All @@ -271,7 +241,7 @@ public function guessPatternForConstraint(Constraint $constraint)
case 'Symfony\Component\Validator\Constraints\Min':
return new ValueGuess(sprintf('.{%s,}', strlen((string) $constraint->limit)), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Range':
case 'Symfony\Component\Validator\Constraints\Size':
return new ValueGuess(sprintf('.{%s,%s}', strlen((string) $constraint->min), strlen((string) $constraint->max)), Guess::LOW_CONFIDENCE);

case 'Symfony\Component\Validator\Constraints\Type':
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ CHANGELOG
-----

* added support for `ctype_*` assertions in `TypeValidator`
* added a Range validator for numeric values
* added a Size validator for string & collections
* improved the ImageValidator with min width, max width, min height, and max height constraints
* added support for MIME with wildcard in FileValidator
* changed Collection validator to add "missing" and "extra" errors to
Expand All @@ -23,3 +21,5 @@ CHANGELOG
* [BC BREAK] collections in fields annotated with `Valid` are not traversed
recursively anymore by default. `Valid` contains a new property `deep`
which enables the BC behavior.
* added MinCount and MaxCount constraint
* deprecated the Size constraint
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@
*
* @api
*/
class Range extends Constraint
class MaxCount extends Constraint
{
public $minMessage = 'This value should be {{ limit }} or more.';
public $maxMessage = 'This value should be {{ limit }} or less.';
public $invalidMessage = 'This value should be a valid number.';
public $min;
public $max;
public $message = 'This collection should contain {{ limit }} elements or less.';
public $limit;

/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'limit';
}

/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
return array('min', 'max');
return array('limit');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/
class RangeValidator extends ConstraintValidator
class MaxCountValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @return Boolean Whether or not the value is valid
*
* @api
*/
public function validate($value, Constraint $constraint)
Expand All @@ -37,28 +34,17 @@ public function validate($value, Constraint $constraint)
return;
}

if (!is_numeric($value)) {
$this->context->addViolation($constraint->invalidMessage, array(
'{{ value }}' => $value,
));

return;
if (!is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or \Countable');
}

if ($value > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->max,
));

return;
}
$count = count($value);

if ($value < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ value }}' => $value,
'{{ limit }}' => $constraint->min,
));
if ($count > $constraint->limit) {
$this->context->addViolation($constraint->message, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->limit,
), $value, (int) $constraint->limit);
}
}
}
41 changes: 41 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MinCount.php
Original file line number Diff line number Diff line change
@@ -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 MinCount extends Constraint
{
public $message = 'This collection should contain {{ limit }} elements or more.';
public $limit;

/**
* {@inheritDoc}
*/
public function getDefaultOption()
{
return 'limit';
}

/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
return array('limit');
}
}
50 changes: 50 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MinCountValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MinCountValidator extends ConstraintValidator
{
/**
* Checks if the passed value is valid.
*
* @param mixed $value The value that should be validated
* @param Constraint $constraint The constraint for the validation
*
* @throws UnexpectedTypeException If the given value is no array or \Countable.
*/
public function validate($value, Constraint $constraint)
{
if (null === $value) {
return;
}

if (!is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or \Countable');
}

$count = count($value);

if ($count < $constraint->limit) {
$this->context->addViolation($constraint->message, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->limit,
), $value, (int) $constraint->limit);
}
}
}
71 changes: 10 additions & 61 deletions src/Symfony/Component/Validator/Constraints/Size.php
179B
Original file line number Diff line number Diff line change
Expand Up @@ -17,73 +17,22 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Size extends Constraint
{
const TYPE_STRING = 'string';
const TYPE_COLLECTION = 'collection';

public $minMessage;
public $maxMessage;
public $exactMessage;
public $type;
public $minMessage = 'This value should be {{ limit }} or more';
public $maxMessage = 'This value should be {{ limit }} or less';
public $invalidMessage = 'This value should be a valid number';
public $min;
public $max;
public $charset = 'UTF-8';

private $stringMinMessage = 'This value is too short. It should have {{ limit }} characters or more.';
private $stringMaxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
private $stringExactMessage = 'This value should have exactly {{ limit }} characters.';

private $collectionMinMessage = 'This collection should contain {{ limit }} elements or more.';
private $collectionMaxMessage = 'This collection should contain {{ limit }} elements or less.';
private $collectionExactMessage = 'This collection should contain exactly {{ limit }} elements.';

public function getMinMessage($type)
/**
* {@inheritDoc}
*/
public function getRequiredOptions()
{
if (null !== $this->minMessage) {
return $this->minMessage;
}

switch ($type) {
case static::TYPE_STRING:
return $this->stringMinMessage;
case static::TYPE_COLLECTION:
return $this->collectionMinMessage;
default:
throw new \InvalidArgumentException('Invalid type specified.');
}
}

public function getMaxMessage($type)
{
if (null !== $this->maxMessage) {
return $this->maxMessage;
}

switch ($type) {
case static::TYPE_STRING:
return $this->stringMaxMessage;
case static::TYPE_COLLECTION:
return $this->collectionMaxMessage;
default:
throw new \InvalidArgumentException('Invalid type specified.');
}
}

public function getExactMessage($type)
{
if (null !== $this->exactMessage) {
return $this->exactMessage;
}

switch ($type) {
case static::TYPE_STRING:
return $this->stringExactMessage;
case static::TYPE_COLLECTION:
return $this->collectionExactMessage;
default:
throw new \InvalidArgumentException('Invalid type specified.');
}
return array('min', 'max');
}
}
Loading
0