浏览代码

[Validator] fixed Min and Max validator when the input value is not a number (now return an error message instead of an exception which does not make sense in this context)

Fabien Potencier 14 年之前
父节点
当前提交
88d915d175

+ 1 - 0
src/Symfony/Component/Validator/Constraints/Max.php

@@ -17,6 +17,7 @@ use Symfony\Component\Validator\Constraint;
 class Max extends Constraint
 {
     public $message = 'This value should be {{ limit }} or less';
+    public $invalidMessage = 'This value should be a valid number';
     public $limit;
 
     /**

+ 6 - 1
src/Symfony/Component/Validator/Constraints/MaxValidator.php

@@ -24,7 +24,12 @@ class MaxValidator extends ConstraintValidator
         }
 
         if (!is_numeric($value)) {
-            throw new UnexpectedTypeException($value, 'numeric');
+            $this->setMessage($constraint->invalidMessage, array(
+                '{{ value }}' => $value,
+                '{{ limit }}' => $constraint->limit,
+            ));
+
+            return false;
         }
 
         if ($value > $constraint->limit) {

+ 1 - 0
src/Symfony/Component/Validator/Constraints/Min.php

@@ -17,6 +17,7 @@ use Symfony\Component\Validator\Constraint;
 class Min extends Constraint
 {
     public $message = 'This value should be {{ limit }} or more';
+    public $invalidMessage = 'This value should be a valid number';
     public $limit;
 
     /**

+ 6 - 1
src/Symfony/Component/Validator/Constraints/MinValidator.php

@@ -24,7 +24,12 @@ class MinValidator extends ConstraintValidator
         }
 
         if (!is_numeric($value)) {
-            throw new UnexpectedTypeException($value, 'numeric');
+            $this->setMessage($constraint->invalidMessage, array(
+                '{{ value }}' => $value,
+                '{{ limit }}' => $constraint->limit,
+            ));
+
+            return false;
         }
 
         if ($value < $constraint->limit) {

+ 1 - 7
tests/Symfony/Tests/Component/Validator/Constraints/MaxValidatorTest.php

@@ -33,13 +33,6 @@ class MaxValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Max(array('limit' => 10))));
     }
 
-    public function testExpectsNumericType()
-    {
-        $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
-
-        $this->validator->isValid(new \stdClass(), new Max(array('limit' => 10)));
-    }
-
     /**
      * @dataProvider getValidValues
      */
@@ -73,6 +66,7 @@ class MaxValidatorTest extends \PHPUnit_Framework_TestCase
         return array(
             array(10.00001),
             array('10.00001'),
+            array(new \stdClass()),
         );
     }
 

+ 1 - 7
tests/Symfony/Tests/Component/Validator/Constraints/MinValidatorTest.php

@@ -28,13 +28,6 @@ class MinValidatorTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue($this->validator->isValid(null, new Min(array('limit' => 10))));
     }
 
-    public function testExpectsNumericType()
-    {
-        $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
-
-        $this->validator->isValid(new \stdClass(), new Min(array('limit' => 10)));
-    }
-
     /**
      * @dataProvider getValidValues
      */
@@ -68,6 +61,7 @@ class MinValidatorTest extends \PHPUnit_Framework_TestCase
         return array(
             array(9.999999),
             array('9.999999'),
+            array(new \stdClass()),
         );
     }