Skip to content

Commit

Permalink
merged branch stloyd/repeatedtype (PR #1453)
Browse files Browse the repository at this point in the history
Commits
-------

67c33a8 Rebased with master, and fixed wrong behavior with proper tests coverage
f8a6a4b Be sure that both fields have same value for required option in RepeatedType
0679220 Additional test coverage for changes in RepeatedType
b23d47d moved options test form from class->method scope
5fe5556 fixed accidental permission change
a969434 [Form] fixed CS, merged options, added tests
8819db3 [Form] Allow setting different options to repeating fields

Discussion
----------

[2.1] [Form] Allow setting different options at RepeatedType fields

This an test covered version of #1348 (rebased with master).

---------------------------------------------------------------------------

by stloyd at 2011/06/27 04:18:19 -0700

@fabpot What do you think about this ? I'm just not sure that we should allow setting `required` per field, IMO better would be forcing this option from default `$options['options']` and ignore that field in `$options['first_options']` and/or `$options['second_options']`.

---------------------------------------------------------------------------

by stloyd at 2011/07/02 00:00:04 -0700

@fabpot ping.

---------------------------------------------------------------------------

by fabpot at 2011/07/06 05:45:56 -0700

Let's discuss this new feature for 2.1.

---------------------------------------------------------------------------

by stloyd at 2011/08/24 01:12:59 -0700

Rebased with master.

---------------------------------------------------------------------------

by stof at 2011/09/04 05:02:42 -0700

@fabpot What do you think about this feature ? It is now time to discuss it :)

---------------------------------------------------------------------------

by fabpot at 2011/09/22 00:18:29 -0700

Tests do not pass.

---------------------------------------------------------------------------

by stloyd at 2011/09/24 01:54:42 -0700

@fabpot Should be ok now.
  • Loading branch information
fabpot committed Sep 24, 2011
2 parents 85ba3d0 + 67c33a8 commit c832b71
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 9 deletions.
20 changes: 13 additions & 7 deletions src/Symfony/Component/Form/Extension/Core/Type/RepeatedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ class RepeatedType extends AbstractType
*/
public function buildForm(FormBuilder $builder, array $options)
{
// Overwrite required option for child fields
$options['first_options']['required'] = $options['required'];
$options['second_options']['required'] = $options['required'];

$builder
->appendClientTransformer(new ValueToDuplicatesTransformer(array(
$options['first_name'],
$options['second_name'],
)))
->add($options['first_name'], $options['type'], $options['options'])
->add($options['second_name'], $options['type'], $options['options'])
->add($options['first_name'], $options['type'], array_merge($options['options'], $options['first_options']))
->add($options['second_name'], $options['type'], array_merge($options['options'], $options['second_options']))
;
}

Expand All @@ -38,11 +42,13 @@ public function buildForm(FormBuilder $builder, array $options)
public function getDefaultOptions(array $options)
{
return array(
'type' => 'text',
'options' => array(),
'first_name' => 'first',
'second_name' => 'second',
'error_bubbling' => false,
'type' => 'text',
'options' => array(),
'first_options' => array(),
'second_options' => array(),
'first_name' => 'first',
'second_name' => 'second',
'error_bubbling' => false,
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Symfony/Tests/Component/Form/AbstractDivLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,32 @@ public function testRepeated()
);
}

public function testRepeatedWithCustomOptions()
{
$form = $this->factory->createNamed('repeated', 'name', null, array(
'first_options' => array('label' => 'Test', 'required' => false),
'second_options' => array('label' => 'Test2')
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./div
[
./label[@for="name_first"][.="[trans]Test[/trans]"]
/following-sibling::input[@type="text"][@id="name_first"][@required="required"]
]
/following-sibling::div
[
./label[@for="name_second"][.="[trans]Test2[/trans]"]
/following-sibling::input[@type="text"][@id="name_second"][@required="required"]
]
]
[count(.//input)=2]
'
);
}

public function testSearchInputName()
{
$form = $this->factory->createNamedBuilder('form', 'full')
Expand Down
35 changes: 33 additions & 2 deletions tests/Symfony/Tests/Component/Form/AbstractTableLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,45 @@ public function testRepeated()
./td
[./label[@for="name_first"]]
/following-sibling::td
[./input[@id="name_first"]]
[./input[@type="text"][@id="name_first"]]
]
/following-sibling::tr
[
./td
[./label[@for="name_second"]]
/following-sibling::td
[./input[@id="name_second"]]
[./input[@type="text"][@id="name_second"]]
]
]
[count(.//input)=2]
'
);
}

public function testRepeatedWithCustomOptions()
{
$form = $this->factory->createNamed('repeated', 'name', 'foobar', array(
'type' => 'password',
'first_options' => array('label' => 'Test', 'required' => false),
'second_options' => array('label' => 'Test2')
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/table
[
./tr
[
./td
[./label[@for="name_first"][.="[trans]Test[/trans]"]]
/following-sibling::td
[./input[@type="password"][@id="name_first"][@required="required"]]
]
/following-sibling::tr
[
./td
[./label[@for="name_second"][.="[trans]Test2[/trans]"]]
/following-sibling::td
[./input[@type="password"][@id="name_second"][@required="required"]]
]
]
[count(.//input)=2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,47 @@ public function testSetData()
$this->assertEquals('foobar', $this->form['second']->getData());
}

public function testSetOptions()
{
$form = $this->factory->create('repeated', null, array(
'type' => 'field',
'options' => array('label' => 'Global'),
));

$this->assertEquals('Global', $form['first']->getAttribute('label'));
$this->assertEquals('Global', $form['second']->getAttribute('label'));
$this->assertTrue($form['first']->isRequired());
$this->assertTrue($form['second']->isRequired());
}

public function testSetOptionsPerField()
{
$form = $this->factory->create('repeated', null, array(
'type' => 'field',
'first_options' => array('label' => 'Test', 'required' => false),
'second_options' => array('label' => 'Test2')
));

$this->assertEquals('Test', $form['first']->getAttribute('label'));
$this->assertEquals('Test2', $form['second']->getAttribute('label'));
$this->assertTrue($form['first']->isRequired());
$this->assertTrue($form['second']->isRequired());
}

public function testSetOptionsPerFieldAndOverwrite()
{
$form = $this->factory->create('repeated', null, array(
'type' => 'field',
'options' => array('label' => 'Label'),
'second_options' => array('label' => 'Second label')
));

$this->assertEquals('Label', $form['first']->getAttribute('label'));
$this->assertEquals('Second label', $form['second']->getAttribute('label'));
$this->assertTrue($form['first']->isRequired());
$this->assertTrue($form['second']->isRequired());
}

public function testSubmitUnequal()
{
$input = array('first' => 'foo', 'second' => 'bar');
Expand Down

0 comments on commit c832b71

Please sign in to comment.