Skip to content

Commit

Permalink
[Form] Fields with the name '0' are now possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek committed Jul 4, 2010
1 parent 34dd0ea commit 1c7b459
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/Symfony/Components/Form/Field.php
Expand Up @@ -91,7 +91,7 @@ protected function getTransformedData()
*/
public function setPropertyPath($propertyPath)
{
$this->propertyPath = empty($propertyPath) ? null : new PropertyPath($propertyPath);
$this->propertyPath = $propertyPath === null || $propertyPath === '' ? null : new PropertyPath($propertyPath);
}

/**
Expand Down Expand Up @@ -454,7 +454,6 @@ protected function reverseTransform($value)
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));
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Components/Form/PropertyPath.php
Expand Up @@ -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');
}

Expand All @@ -55,7 +55,7 @@ public function __construct($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 {
Expand Down
29 changes: 29 additions & 0 deletions tests/Symfony/Tests/Components/Form/FieldTest.php
Expand Up @@ -8,6 +8,7 @@
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;
Expand All @@ -22,6 +23,34 @@ protected function setUp()
$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));
Expand Down
14 changes: 14 additions & 0 deletions tests/Symfony/Tests/Components/Form/PropertyPathTest.php
Expand Up @@ -37,6 +37,13 @@ public function testValidPropertyPath()
$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');
Expand Down Expand Up @@ -72,6 +79,13 @@ public function testInvalidPropertyPath_empty()
new PropertyPath('');
}

public function testInvalidPropertyPath_null()
{
$this->setExpectedException('Symfony\Components\Form\Exception\InvalidPropertyPathException');

new PropertyPath(null);
}

public function testNextThrowsExceptionIfNoNextElement()
{
$path = new PropertyPath('property');
Expand Down

0 comments on commit 1c7b459

Please sign in to comment.