Bladeren bron

[Form] Fields with the name '0' are now possible

Bernhard Schussek 15 jaren geleden
bovenliggende
commit
1c7b459776

+ 1 - 2
src/Symfony/Components/Form/Field.php

@@ -91,7 +91,7 @@ abstract class Field extends Configurable implements FieldInterface
      */
     public function setPropertyPath($propertyPath)
     {
-        $this->propertyPath = empty($propertyPath) ? null : new PropertyPath($propertyPath);
+        $this->propertyPath = $propertyPath === null || $propertyPath === '' ? null : new PropertyPath($propertyPath);
     }
 
     /**
@@ -454,7 +454,6 @@ abstract class Field extends Configurable implements FieldInterface
     public function updateFromObject(&$objectOrArray)
     {
         // TODO throw exception if not object or array
-
         if ($this->propertyPath !== null) {
             $this->propertyPath->rewind();
             $this->setData($this->readPropertyPath($objectOrArray, $this->propertyPath));

+ 2 - 2
src/Symfony/Components/Form/PropertyPath.php

@@ -43,7 +43,7 @@ class PropertyPath
      */
     public function __construct($propertyPath)
     {
-        if (empty($propertyPath)) {
+        if ($propertyPath === '' || $propertyPath === null) {
             throw new InvalidPropertyPathException('The property path must not be empty');
         }
 
@@ -55,7 +55,7 @@ class PropertyPath
         $pattern = '/^((\w+)|\[(\w+)\])(.*)/';
 
         while (preg_match($pattern, $remaining, $matches)) {
-            if (!empty($matches[2])) {
+            if ($matches[2] !== '') {
                 $this->elements[] = $matches[2];
                 $this->isProperty[] = true;
             } else {

+ 29 - 0
tests/Symfony/Tests/Components/Form/FieldTest.php

@@ -8,6 +8,7 @@ require_once __DIR__ . '/Fixtures/InvalidField.php';
 require_once __DIR__ . '/Fixtures/RequiredOptionsField.php';
 
 use Symfony\Components\Form\ValueTransformer\ValueTransformerInterface;
+use Symfony\Components\Form\PropertyPath;
 use Symfony\Tests\Components\Form\Fixtures\Author;
 use Symfony\Tests\Components\Form\Fixtures\TestField;
 use Symfony\Tests\Components\Form\Fixtures\InvalidField;
@@ -22,6 +23,34 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->field = new TestField('title');
     }
 
+    public function testGetPropertyPath_defaultPath()
+    {
+        $field = new TestField('title');
+
+        $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
+    }
+
+    public function testGetPropertyPath_pathIsZero()
+    {
+        $field = new TestField('title', array('property_path' => '0'));
+
+        $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
+    }
+
+    public function testGetPropertyPath_pathIsEmpty()
+    {
+        $field = new TestField('title', array('property_path' => ''));
+
+        $this->assertEquals(null, $field->getPropertyPath());
+    }
+
+    public function testGetPropertyPath_pathIsNull()
+    {
+        $field = new TestField('title', array('property_path' => null));
+
+        $this->assertEquals(null, $field->getPropertyPath());
+    }
+
     public function testPassRequiredAsOption()
     {
         $field = new TestField('title', array('required' => false));

+ 14 - 0
tests/Symfony/Tests/Components/Form/PropertyPathTest.php

@@ -37,6 +37,13 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($path->isIndex());
     }
 
+    public function testValidPropertyPath_zero()
+    {
+        $path = new PropertyPath('0');
+
+        $this->assertEquals('0', $path->getCurrent());
+    }
+
     public function testToString()
     {
         $path = new PropertyPath('reference.traversable[index].property');
@@ -72,6 +79,13 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
         new PropertyPath('');
     }
 
+    public function testInvalidPropertyPath_null()
+    {
+        $this->setExpectedException('Symfony\Components\Form\Exception\InvalidPropertyPathException');
+
+        new PropertyPath(null);
+    }
+
     public function testNextThrowsExceptionIfNoNextElement()
     {
         $path = new PropertyPath('property');