Skip to content

[Form] Fixed collapsed choice attributes #19020

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
merged 1 commit into from
Jun 13, 2016
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
<?php foreach ($choice_attr as $k => $v): ?>
<?php if ($v === true): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
<?php elseif ($v !== false): ?>
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?>
<?php endif ?>
<?php endforeach ?>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
</optgroup>
<?php else: ?>
<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>
<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>
<?php endif ?>
<?php endforeach ?>
62 changes: 62 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,68 @@ public function testSingleChoice()
);
}

public function testSingleChoiceAttributesWithMainAttributes()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
'choices_as_values' => true,
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'bar&baz'),
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
'/select
[@name="name"]
[@class="bar&baz form-control"]
[not(@required)]
[
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
);
}

public function testSingleExpandedChoiceAttributesWithMainAttributes()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
'choices_as_values' => true,
'multiple' => false,
'expanded' => true,
'attr' => array('class' => 'bar&baz'),
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
'/div
[@class="bar&baz"]
[
./div
[@class="radio"]
[
./label
[.=" [trans]Choice&A[/trans]"]
[
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
]
]
/following-sibling::div
[@class="radio"]
[
./label
[.=" [trans]Choice&B[/trans]"]
[
./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
]
]
/following-sibling::input[@type="hidden"][@id="name__token"]
]
'
);
}

public function testSelectWithSizeBiggerThanOneCanBeRequired()
{
$form = $this->factory->createNamed('name', 'choice', null, array(
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,55 @@ public function testSingleChoiceAttributes()
);
}

public function testSingleChoiceAttributesWithMainAttributes()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
'choices_as_values' => true,
'multiple' => false,
'expanded' => false,
'attr' => array('class' => 'bar&baz'),
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
'/select
[@name="name"]
[@class="bar&baz"]
[not(@required)]
[
./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
/following-sibling::option[@value="&b"][not(@class)][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
);
}

public function testSingleExpandedChoiceAttributesWithMainAttributes()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'),
'choices_as_values' => true,
'multiple' => false,
'expanded' => true,
'attr' => array('class' => 'bar&baz'),
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'bar&baz')),
'/div
[@class="bar&baz"]
[
./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked]
/following-sibling::label[@for="name_0"][.="[trans]Choice&A[/trans]"]
/following-sibling::input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
/following-sibling::label[@for="name_1"][.="[trans]Choice&B[/trans]"]
/following-sibling::input[@type="hidden"][@id="name__token"]
]
[count(./input)=3]
'
);
}

public function testSingleChoiceWithPreferred()
{
$form = $this->factory->createNamed('name', 'choice', '&a', array(
Expand Down