Explorar el Código

[Validator] added support for '0' as default constraint option value

avalanche123 hace 15 años
padre
commit
f2c4f20e70

+ 1 - 1
src/Symfony/Components/Validator/Constraint.php

@@ -67,7 +67,7 @@ class Constraint
                     $invalidOptions[] = $option;
                 }
             }
-        } else if ($options) {
+        } else if ($options !== null && ! (is_array($options) && count($options) === 0)) {
             $option = $this->defaultOption();
 
             if (is_null($option)) {

+ 11 - 0
tests/Symfony/Tests/Components/Validator/ConstraintTest.php

@@ -82,4 +82,15 @@ class ConstraintTest extends \PHPUnit_Framework_TestCase
         $constraint->addImplicitGroupName('Foo');
         $this->assertEquals(array('Default', 'Foo'), $constraint->groups);
     }
+
+    public function testAllowsSettingZeroRequiredPropertyValue()
+    {
+        $constraint = new ConstraintA(0);
+        $this->assertEquals(0, $constraint->property2);
+    }
+
+    public function testCanCreateConstraintWithNoDefaultOptionAndEmptyArray()
+    {
+        new ConstraintB(array());
+    }
 }