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

This commit is contained in:
Bernhard Schussek 2010-07-04 17:03:03 +02:00
parent 34dd0ea25b
commit 1c7b459776
4 changed files with 46 additions and 4 deletions

View File

@ -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));

View File

@ -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 {

View File

@ -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));

View File

@ -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');