8000 [Form] Fixed collapsed choice attributes · symfony/symfony@445dcc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 445dcc8

Browse files
committed
[Form] Fixed collapsed choice attributes
1 parent 326465d commit 445dcc8

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"
2+
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
3+
<?php foreach ($choice_attr as $k => $v): ?>
4+
<?php if ($v === true): ?>
5+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
6+
<?php elseif ($v !== false): ?>
7+
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
8+
<?php endif ?>
9+
<?php endforeach ?>

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
99
</optgroup>
1010
<?php else: ?>
11-
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($choice->label, array(), $choice_translation_domain) : $choice->label) ?></option>
11+
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $formHelper->block($form, 'choice_attributes', array('choice_attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($choice->label, array(), $choice_translation_domain) : $choice->label) ?></option>
1212
<?php endif ?>
1313
<?php endforeach ?>

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,68 @@ public function testSingleChoice()
232232
);
233233
}
234234

235+
public function testSingleChoiceAttributesWithMainAttributes()
236+
{
237+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
238+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
239+
'choices_as_values' => true,
240+
'multiple' => false,
241+
'expanded' => false,
242+
'attr' => array('class' => 'bar&baz'),
243+
));
244+
245+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
246+
'/select
247+
[@name="name"]
248+
[@class="bar&baz form-control"]
249+
[not(@required)]
250+
[
251+
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
252+
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
253+
]
254+
[count(./option)=2]
255+
'
256+
);
257+
}
258+
259+
public function testSingleExpandedChoiceAttributesWithMainAttributes()
260+
{
261+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
262+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
263+
'choices_as_values' => true,
264+
'multiple' => false,
265+
'expanded' => true,
266+
'attr' => array('class' => 'bar&baz'),
267+
));
268+
269+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
270+
'/div
271+
[@class="bar&baz"]
272+
[
273+
./div
274+
[@class="radio"]
275+
[
276+
./label
277+
[.=" [trans]Choice&A[/trans]"]
278+
[
279+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
280+
]
281+
]
282+
/following-sibling::div
283+
[@class="radio"]
284+
[
285+
./label
286+
[.=" [trans]Choice&B[/trans]"]
287+
[
288+
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
289+
]
290+
]
291+
/following-sibling::input[@type="hidden"][@id="name__token"]
292+
]
293+
'
294+
);
295+
}
296+
235297
public function testSelectWithSizeBiggerThanOneCanBeRequired()
236298
{
237299
$form = $this->factory->createNamed('name', 'choice', null, array(

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,55 @@ public function testSingleChoiceAttributes()
596596
);
597597
}
598598

599+
public function testSingleChoiceAttributesWithMainAttributes()
600+
{
601+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
602+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
603+
'choices_as_values' => true,
604+
'multiple' => false,
605+
'expanded' => false,
606+
'attr' => array('class' => 'bar&baz'),
607+
));
608+
609+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
610+
'/select
611+
[@name="name"]
612+
[@class="bar&baz"]
613+
[not(@required)]
614+
[
615+
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
616+
/following-sibling::option[@value="&b"][not(@class)][not(@selected)][.="[trans]Choice&B[/trans]"]
617+
]
618+
[count(./option)=2]
619+
'
620+
);
621+
}
622+
623+
public function testSingleExpandedChoiceAttributesWithMainAttributes()
624+
{
625+
$form = $this->factory->createNamed('name', 'choice', '&a', array(
626+
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
627+
'choices_as_values' => true,
628+
'multiple' => false,
629+
'expanded' => true,
630+
'attr' => array('class' => 'bar&baz'),
631+
));
632+
633+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
634+
'/div
635+
[@class="bar&baz"]
636+
[
637+
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
638+
/following-sibling::label[@for="name_0"][.="[trans]Choice&A[/trans]"]
639+
/following-sibling::input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
640+
/following-sibling::label[@for="name_1"][.="[trans]Choice&B[/trans]"]
641+
/following-sibling::input[@type="hidden"][@id="name__token"]
642+
]
643+
[count(./input)=3]
644+
'
645+
);
646+
}
647+
599648
public function testSingleChoiceWithPreferred()
600649
{
601650
$form = $this->factory->createNamed('name', 'choice', '&a', array(

0 commit comments

Comments
 (0)
0