Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a3b3a78

Browse files
committed
[Validator] added a constraint that runs an expression
1 parent 1bcfb40 commit a3b3a78

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ CHANGELOG
44
2.4.0
55
-----
66

7-
* added `minRatio`, `maxRatio`, `allowSquare`, `allowLandscape`, and `allowPortrait` to Image validator
7+
* added a constraint the uses the expression language
8+
* added `minRatio`, `maxRatio`, `allowSquare`, `allowLandscape`, and `allowPortrait` to Image validator
89

910
2.3.0
1011
-----
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
*
19+
* @author Fabien Potencier <[email protected]>
20+
*/
21+
class Condition extends Constraint
22+
{
23+
public $message = 'This value is not valid.';
24+
public $condition;
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function getDefaultOption()
30+
{
31+
return 'condition';
32+
}
33+
34+
/**
35+
* {@inheritDoc}
36+
*/
37+
public function getRequiredOptions()
38+
{
39+
return array('condition');
40+
}
41+
/**
42+
* {@inheritDoc}
43+
*/
44+
public function getTargets()
45+
{
46+
return self::CLASS_CONSTRAINT;
47+
}
48+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
17+
18+
/**
19+
* @author Fabien Potencier <[email protected]>
20+
*/
21+
class ConditionValidator extends ConstraintValidator
22+
{
23+
private $expressionLanguage;
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
public function validate($object, Constraint $constraint)
29+
{
30+
if (null === $object) {
31+
return;
32+
}
33+
34+
if (!$this->getExpressionLanguage()->evaluate($constraint->condition, array('this' => $object))) {
35+
$this->context->addViolation($constraint->message);
36+
}
37+
}
38+
39+
private function getExpressionLanguage()
40+
{
41+
if (null === $this->expressionLanguage) {
42+
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
43+
throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
44+
}
45+
$this->expressionLanguage = new ExpressionLanguage();
46+
}
47+
48+
return $this->expressionLanguage;
49+
}
50+
}

0 commit comments

Comments
 (0)