[Form] Choice children can be template customized like collection

This commit is contained in:
Adrien Brault 2013-09-13 08:21:38 -07:00
parent 42f4b6db7b
commit 4e6b27fe90
6 changed files with 46 additions and 12 deletions

View File

@ -6,11 +6,20 @@
{% endspaceless %}
{% endblock _text_id_widget %}
{% block _name_entry_label %}
{% block _names_entry_label %}
{% spaceless %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label>Custom label: {{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock _name_entry_label %}
{% endblock _names_entry_label %}
{% block _name_c_entry_label %}
{% spaceless %}
{% if label is empty %}
{% set label = name|humanize %}
{% endif %}
<label>Custom name label: {{ label|trans({}, translation_domain) }}</label>
{% endspaceless %}
{% endblock _name_c_entry_label %}

View File

@ -0,0 +1,2 @@
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
<label>Custom name label: <?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>

View File

@ -256,6 +256,7 @@ class ChoiceType extends AbstractType
'value' => $choiceView->value,
'label' => $choiceView->label,
'translation_domain' => $options['translation_domain'],
'block_name' => 'entry',
);
if ($options['multiple']) {

View File

@ -283,7 +283,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
public function testCollection()
{
$form = $this->factory->createNamed('name', 'collection', array('a', 'b'), array(
$form = $this->factory->createNamed('names', 'collection', array('a', 'b'), array(
'type' => 'text',
));
@ -305,7 +305,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
array('title' => 'a'),
array('title' => 'b'),
);
$form = $this->factory->createNamed('name', 'collection', $data, array(
$form = $this->factory->createNamed('names', 'collection', $data, array(
'type' => new AlternatingRowType(),
));
@ -323,13 +323,13 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
public function testEmptyCollection()
{
$form = $this->factory->createNamed('name', 'collection', array(), array(
$form = $this->factory->createNamed('names', 'collection', array(), array(
'type' => 'text',
));
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[./input[@type="hidden"][@id="name__token"]]
[./input[@type="hidden"][@id="names__token"]]
[count(./div)=0]
'
);
@ -670,7 +670,7 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
public function testCollectionRowWithCustomBlock()
{
$collection = array('one', 'two', 'three');
$form = $this->factory->createNamedBuilder('name', 'collection', $collection)
$form = $this->factory->createNamedBuilder('names', 'collection', $collection)
->getForm();
$this->assertWidgetMatchesXpath($form->createView(), array(),
@ -684,6 +684,28 @@ abstract class AbstractDivLayoutTest extends AbstractLayoutTest
);
}
/**
* The block "_name_c_entry_label" should be overridden in the theme of the
* implemented driver.
*/
public function testChoiceRowWithCustomBlock()
{
$form = $this->factory->createNamedBuilder('name_c', 'choice', 'a', array(
'choices' => array('a' => 'ChoiceA', 'b' => 'ChoiceB'),
'expanded' => true,
))
->getForm();
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
./label[.="Custom name label: [trans]ChoiceA[/trans]"]
/following-sibling::label[.="Custom name label: [trans]ChoiceB[/trans]"]
]
'
);
}
public function testFormEndWithRest()
{
$view = $this->factory->createNamedBuilder('name', 'form')

View File

@ -193,7 +193,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
public function testCollection()
{
$form = $this->factory->createNamed('name', 'collection', array('a', 'b'), array(
$form = $this->factory->createNamed('names', 'collection', array('a', 'b'), array(
'type' => 'text',
));
@ -202,7 +202,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
[
./tr[./td/input[@type="text"][@value="a"]]
/following-sibling::tr[./td/input[@type="text"][@value="b"]]
/following-sibling::tr[@style="display: none"][./td[@colspan="2"]/input[@type="hidden"][@id="name__token"]]
/following-sibling::tr[@style="display: none"][./td[@colspan="2"]/input[@type="hidden"][@id="names__token"]]
]
[count(./tr[./td/input])=3]
'
@ -211,13 +211,13 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
public function testEmptyCollection()
{
$form = $this->factory->createNamed('name', 'collection', array(), array(
$form = $this->factory->createNamed('names', 'collection', array(), array(
'type' => 'text',
));
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/table
[./tr[@style="display: none"][./td[@colspan="2"]/input[@type="hidden"][@id="name__token"]]]
[./tr[@style="display: none"][./td[@colspan="2"]/input[@type="hidden"][@id="names__token"]]]
[count(./tr[./td/input])=1]
'
);
@ -439,7 +439,7 @@ abstract class AbstractTableLayoutTest extends AbstractLayoutTest
public function testCollectionRowWithCustomBlock()
{
$collection = array('one', 'two', 'three');
$form = $this->factory->createNamedBuilder('name', 'collection', $collection)
$form = $this->factory->createNamedBuilder('names', 'collection', $collection)
->getForm();
$this->assertWidgetMatchesXpath($form->createView(), array(),