8000 [Form] Outstanding patches for Choice/Collection fields (cleaned up) by jmikola · Pull Request #17 · fabpot/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Outstanding patches for Choice/Collection fields (cleaned up) #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
7 commits merged into from
Sep 10, 2010
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Symfony/Component/Form/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ protected function configure()

foreach ($this->getOption('preferred_choices') as $choice) {
$this->add($this->newChoiceField($choice, $choices[$choice]));
unset($choices[$choice]);
}

foreach ($this->getOption('choices') as $choice => $value) {
$this->add($this->newChoiceField($choice, $value));
if (!isset($this->preferredChoices[$choice])) {
$this->add($this->newChoiceField($choice, $value));
}
}
} else {
$this->setFieldMode(self::FIELD);
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Form/CollectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public function setData($collection)
throw new UnexpectedTypeException('The data must be an array');
}

foreach ($collection as $name => $value) {
$this->add($this->newField($name, $name));
foreach ($this as $name => $field) {
if (!$this->getOption('modifiable') || $name != '$$key$$') {
$this->remove($name);
}
}

parent::setData($collection);
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/FieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,10 @@ public function addError($message, PropertyPath $path = null, $type = null)
if ($type === self::FIELD_ERROR && $path->hasNext()) {
$path->next();

if ($path->isProperty() && $path->getCurrent() === 'fields') {
$path->next();
}

if ($this->has($path->getCurrent()) && !$this->get($path->getCurrent())->isHidden()) {
$this->get($path->getCurrent())->addError($message, $path, $type);

Expand Down
88 changes: 84 additions & 4 deletions tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
)
);

protected $numericChoices = array(
0 => 'Bernhard',
1 => 'Fabien',
2 => 'Kris',
3 => 'Jon',
4 => 'Roman',
);

public function testConfigureChoicesWithArrayObject()
{
$choices = new \ArrayObject($this->choices);

$field = new ChoiceField('name', array(
'multiple' => false,
'expanded' => true,
'choices' => $choices,
'preferred_choices' => $this->preferredChoices,
));

$this->assertEquals($this->choices, $choices->getArrayCopy());
}

public function testBindSingleNonExpanded()
{
$field = new ChoiceField('name', array(
Expand Down Expand Up @@ -241,15 +263,49 @@ public function testRenderMultipleNonExpanded()
public function testBindSingleExpanded()
{
$field = new ChoiceField('name', array(
'multiple' => true,
'expanded' => false,
'multiple' => false,
'expanded' => true,
'choices' => $this->choices,
));

$field->bind('b');

$this->assertEquals('b', $field->getData());
$this->assertEquals('b', $field->getDisplayedData());
$this->assertSame('b', $field->getData());
$this->assertSame(null, $field['a']->getData());
$this->assertSame(true, $field['b']->getData());
$this->assertSame(null, $field['c']->getData());
$this->assertSame(null, $field['d']->getData());
$this->assertSame(null, $field['e']->getData());
$this->assertSame('', $field['a']->getDisplayedData());
$this->assertSame('1', $field['b']->getDisplayedData());
$this->assertSame('', $field['c']->getDisplayedData());
$this->assertSame('', $field['d']->getDisplayedData());
$this->assertSame('', $field['e']->getDisplayedData());
$this->assertSame(array('a' => '', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
}

public function testBindSingleExpandedNumericChoices()
{
$field = new ChoiceField('name', array(
'multiple' => false,
'expanded' => true,
'choices' => $this->numericChoices,
));

$field->bind('1');

$this->assertSame(1, $field->getData());
$this->assertSame(null, $field[0]->getData());
$this->assertSame(true, $field[1]->getData());
$this->assertSame(null, $field[2]->getData());
$this->assertSame(null, $field[3]->getData());
$this->assertSame(null, $field[4]->getData());
$this->assertSame('', $field[0]->getDisplayedData());
$this->assertSame('1', $field[1]->getDisplayedData());
$this->assertSame('', $field[2]->getDisplayedData());
$this->assertSame('', $field[3]->getDisplayedData());
$this->assertSame('', $field[4]->getDisplayedData());
$this->assertSame(array(0 => '', 1 => '1', 2 => '', 3 => '', 4 => ''), $field->getDisplayedData());
}

public function testRenderSingleExpanded()
Expand Down Expand Up @@ -350,6 +406,30 @@ public function testBindMultipleExpanded()
$this->assertSame(array('a' => '1', 'b' => '1', 'c' => '', 'd' => '', 'e' => ''), $field->getDisplayedData());
}

public function testBindMultipleExpandedNumericChoices()
{
$field = new ChoiceField('name', array(
'multiple' => true,
'expanded' => true,
'choices' => $this->numericChoices,
));

$field->bind(array(1 => 1, 2 => 2));

$this->assertSame(array(1, 2), $field->getData());
$this->assertSame(null, $field[0]->getData());
$this->assertSame(true, $field[1]->getData());
$this->assertSame(true, $field[2]->getData());
$this->assertSame(null, $field[3]->getData());
$this->assertSame(null, $field[4]->getData());
$this->assertSame('', $field[0]->getDisplayedData());
$this->assertSame('1', $field[1]->getDisplayedData());
$this->assertSame('1', $field[2]->getDisplayedData());
$this->assertSame('', $field[3]->getDisplayedData());
$this->assertSame('', $field[4]->getDisplayedData());
$this->assertSame(array(0 => '', 1 => '1', 2 => '1', 3 => '', 4 => ''), $field->getDisplayedData());
}

public function testRenderMultipleExpanded()
{
$field = new ChoiceField('name', array(
Expand Down
47 changes: 47 additions & 0 deletions tests/Symfony/Tests/Component/Form/CollectionFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_once __DIR__ . '/Fixtures/TestField.php';

use Symfony\Component\Form\CollectionField;
use Symfony\Component\Form\FieldGroup;
use Symfony\Tests\Component\Form\Fixtures\TestField;


Expand All @@ -26,6 +27,31 @@ public function testSetDataAdjustsSize()
$this->assertEquals(2, count($field));
$this->assertEquals('foo@foo.com', $field[0]->getData());
$this->assertEquals('foo@bar.com', $field[1]->getData());

$field->setData(array('foo@baz.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertFalse(isset($field[1]));
$this->assertEquals(1, count($field));
$this->assertEquals('foo@baz.com', $field[0]->getData());
}

public function testSetDataAdjustsSizeIfModifiable()
{
$field = new CollectionField(new TestField('emails'), array(
'modifiable' => true,
));
$field->setData(array('foo@foo.com', 'foo@bar.com'));

$this->assertTrue($field[0] instanceof TestField);
$this->assertTrue($field[1] instanceof TestField);
$this->assertTrue($field['$$key$$'] instanceof TestField);
$this->assertEquals(3, count($field));

$field->setData(array('foo@baz.com'));
$this->assertTrue($field[0] instanceof TestField);
$this->assertFalse(isset($field[1]));
$this->assertTrue($field['$$key$$'] instanceof TestField);
$this->assertEquals(2, count($field));
}

public function testThrowsExceptionIfObjectIsNotTraversable()
Expand Down Expand Up @@ -96,4 +122,25 @@ public function testResizedIfBoundWithExtraDataAndModifiable()
$this->assertEquals('foo@foo.com', $field[0]->getData());
$this->assertEquals('bar@bar.com', $field[1]->getData());
}

public function testCollectionOfFieldGroupsBoundWithArrayObjectContainingObjects()
{
$fieldGroup = new FieldGroup('name');
$fieldGroup->add(new TestField('first'));
$fieldGroup->add(new TestField('last'));

$field = new CollectionField($fieldGroup);

$nameData = (object) array('first' => 'Foo', 'last' => 'Bar');
$collectionData = new \ArrayObject(array($nameData));
$field->setData($collectionData);

$boundNameData = (object) array('first' => 'Foo', 'last' => 'Baz');
$boundCollectionData = new \ArrayObject(array($nameData));
$field->bind($boundCollectionData);

$this->assertTrue($field->has('0'));
$this->assertFalse($field->has('1'));
$this->assertEquals($boundNameData, $field[0]->getData());
}
}
15 changes: 15 additions & 0 deletions tests/Symfony/Tests/Component/Form/FieldGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ public function testAddErrorMapsFieldValidationErrorsOntoFields()
$group->addError('Message', new PropertyPath('fields[firstName].data'), FieldGroup::FIELD_ERROR);
}

public function testAddErrorMapsFieldValidationErrorsOntoFieldsWithinNestedFieldGroups()
{
$field = $this->createMockField('firstName');
$field->expects($this->once())
->method('addError')
->with($this->equalTo('Message'));

$group = new FieldGroup('author');
$innerGroup = new FieldGroup('names');
$innerGroup->add($field);
$group->add($innerGroup);

$group->addError('Message', new PropertyPath('fields[names].fields[firstName].data'), FieldGroup::FIELD_ERROR);
}

public function testAddErrorKeepsFieldValidationErrorsIfFieldNotFound()
{
$field = $this->createMockField('foo');
Expand Down
0