8000 [Validator] Reintroduced Range constraint and created Count and Length constraints by webmozart · Pull Request #4863 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Reintroduced Range constraint and created Count and Length constraints #4863

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 12, 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
40 changes: 34 additions & 6 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,8 +1136,8 @@
private $recursiveCollection;
```

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

Before:

Expand All @@ -1149,13 +1149,41 @@
After:

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

Before:

```
/** @Assert\Min(2) */
private $numberOfCpus;
```

After:

```
/** @Assert\Range(min = 2) */
private $numberOfCpus;
```

* The `MinLength` and `MaxLength` constraints were deprecated and will be
removed in Symfony 2.3. You should use the new constraint `Length` instead.

Before:

```
/** @Assert\MinLength(8) */
private $password;
```

After:

```
/** @Assert\Length(min = 8) */
private $password;
```

### Session

* Flash messages now return an array based on their type. The old method is
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ 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
* added Count constraint
* added Length constraint
* deprecated the Size constraint and renamed it to Range
* deprecated the Min and Max constraints
* deprecated the MinLength and MaxLength constraints
45 changes: 45 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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\Exception\MissingOptionsException;

/**
* @Annotation
*
* @api
*/
class Count extends Constraint
{
public $minMessage = 'This collection should contain {{ limit }} elements or more.';
public $maxMessage = 'This collection should contain {{ limit }} elements or less.';
public $exactMessage = 'This collection should contain exactly {{ limit }} elements.';
public $min;
public $max;

public function __construct($options = null)
{
if (null !== $options && !is_array($options)) {
$options = array(
'min' => $options,
'max' => $options,
);
}

parent::__construct($options);

if (null === $this->min && null === $this->max) {
throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MaxCountValidator extends ConstraintValidator
class CountValidator 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
*
* @api
*/
public function validate($value, Constraint $constraint)
{
Expand All @@ -40,11 +38,29 @@ public function validate($value, Constraint $constraint)

$count = count($value);

if ($count > $constraint->limit) {
$this->context->addViolation($constraint->message, array(
if ($constraint->min == $constraint->max && $count != $constraint->min) {
$this->context->addViolation($constraint->exactMessage, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);

return;
}

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

return;
}

if (null !== $constraint->min && $count < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ count }}' => $count,
'{{ limit }}' => $constraint->limit,
), $value, (int) $constraint->limit);
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);
}
}
}
46 changes: 46 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Exception\MissingOptionsException;

/**
* @Annotation
*
* @api
*/
class Length extends Constraint
{
public $maxMessage = 'This value is too long. It should have {{ limit }} characters or less.';
public $minMessage = 'This value is too short. It should have {{ limit }} characters or more.';
public $exactMessage = 'This value should have exactly {{ limit }} characters.';
public $max;
public $min;
public $charset = 'UTF-8';

public function __construct($options = null)
{
if (null !== $options && !is_array($options)) {
$options = array(
'min' => $options,
'max' => $options,
);
}

parent::__construct($options);

if (null === $this->min && null === $this->max) {
throw new MissingOptionsException('Either option "min" or "max" must be given for constraint ' . __CLASS__, array('min', 'max'));
}
}
}
74 changes: 74 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LengthValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 LengthValidator 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
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

$stringValue = (string) $value;

if (function_exists('grapheme_strlen') && 'UTF-8' === $constraint->charset) {
$length = grapheme_strlen($stringValue);
} elseif (function_exists('mb_strlen')) {
$length = mb_strlen($stringValue, $constraint->charset);
} else {
$length = strlen($stringValue);
}

if ($constraint->min == $constraint->max && $length != $constraint->min) {
$this->context->addViolation($constraint->exactMessage, array(
'{{ value }}' => $stringValue,
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);

return;
}

if (null !== $constraint->max && $length > $constraint->max) {
$this->context->addViolation($constraint->maxMessage, array(
'{{ value }}' => $stringValue,
'{{ limit }}' => $constraint->max,
), $value, (int) $constraint->max);

return;
}

if (null !== $constraint->min && $length < $constraint->min) {
$this->context->addViolation($constraint->minMessage, array(
'{{ value }}' => $stringValue,
'{{ limit }}' => $constraint->min,
), $value, (int) $constraint->min);
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Max extends Constraint
{
Expand Down
41 changes: 0 additions & 41 deletions src/Symfony/Component/Validator/Constraints/MaxCount.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MaxLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class MaxLength extends Constraint
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class MaxLengthValidator extends ConstraintValidator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/MaxValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class MaxValidator extends ConstraintValidator
{
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* @Annotation
*
* @api
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
class Min extends Constraint
{
Expand Down
Loading
0