diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md
index 14476e1d0a..587c2fe595 100644
--- a/CHANGELOG-2.1.md
+++ b/CHANGELOG-2.1.md
@@ -265,6 +265,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
set to not required now, as stated in the docs
* fixed TimeType and DateTimeType to not display seconds when "widget" is
"single_text" unless "with_seconds" is set to true
+ * checkboxes of in an expanded multiple-choice field don't include the choice
+ in their name anymore. Their names terminate with "[]" now.
### HttpFoundation
diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md
index 996f338f00..a193ddcac3 100644
--- a/UPGRADE-2.1.md
+++ b/UPGRADE-2.1.md
@@ -158,25 +158,11 @@ UPGRADE FROM 2.0 to 2.1
`getChoices()` and `getChoicesByValues()`. For the latter two, no
replacement exists.
- * The strategy for generating the `id` and `name` HTML attributes for choices
- in a choice field has changed.
+ * The strategy for generating the `id` and `name` HTML attributes for
+ checkboxes and radio buttons in a choice field has changed.
Instead of appending the choice value, a generated integer is now appended
- by default. Take care if your JavaScript relies on the old behavior. If you
- can guarantee that your choice values only contain ASCII letters, digits,
- colons and underscores, you can restore the old behavior by setting the
- `index_strategy` choice field option to `ChoiceList::COPY_CHOICE`.
-
- * The strategy for generating the `value` HTML attribute for choices in a
- choice field has changed.
-
- Instead of using the choice value, a generated integer is now stored. Again,
- take care if your JavaScript reads this value. If your choice field is a
- non-expanded single-choice field, or if the choices are guaranteed not to
- contain the empty string '' (which is the case when you added it manually
- or when the field is a single-choice field and is not required), you can
- restore the old behavior by setting the `value_strategy` choice field option
- to `ChoiceList::COPY_CHOICE`.
+ by default. Take care if your JavaScript relies on that.
* In the choice field type's template, the structure of the `choices` variable
has changed.
diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
index aa41d0a330..789423c88b 100644
--- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
+++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
@@ -319,7 +319,7 @@ class EntityChoiceList extends ObjectChoiceList
protected function createValue($entity)
{
if (count($this->identifier) === 1) {
- return current($this->getIdentifierValues($entity));
+ return (string) current($this->getIdentifierValues($entity));
}
return parent::createValue($entity);
diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
index 34d1055f34..1300d3e61d 100644
--- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
+++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
@@ -442,8 +442,8 @@ class EntityTypeTest extends TypeTestCase
$this->assertSame($entity2, $field->getData());
$this->assertFalse($field['1']->getData());
$this->assertTrue($field['2']->getData());
- $this->assertSame('', $field['1']->getClientData());
- $this->assertSame('1', $field['2']->getClientData());
+ $this->assertNull($field['1']->getClientData());
+ $this->assertSame('2', $field['2']->getClientData());
}
public function testSubmitMultipleExpanded()
@@ -462,7 +462,7 @@ class EntityTypeTest extends TypeTestCase
'property' => 'name',
));
- $field->bind(array('1' => '1', '3' => '3'));
+ $field->bind(array('1', '3'));
$expected = new ArrayCollection(array($entity1, $entity3));
@@ -472,8 +472,8 @@ class EntityTypeTest extends TypeTestCase
$this->assertFalse($field['2']->getData());
$this->assertTrue($field['3']->getData());
$this->assertSame('1', $field['1']->getClientData());
- $this->assertSame('', $field['2']->getClientData());
- $this->assertSame('1', $field['3']->getClientData());
+ $this->assertNull($field['2']->getClientData());
+ $this->assertSame('3', $field['3']->getClientData());
}
public function testOverrideChoices()
diff --git a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
index 585ab8913f..254de8385e 100644
--- a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
+++ b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
@@ -304,7 +304,7 @@ class ModelChoiceList extends ObjectChoiceList
protected function createValue($model)
{
if (1 === count($this->identifier)) {
- return current($this->getIdentifierValues($model));
+ return (string) current($this->getIdentifierValues($model));
}
return parent::createValue($model);
diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php
index b9559074a2..cf226ff1e8 100644
--- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php
+++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php
@@ -32,29 +32,6 @@ use Symfony\Component\Form\Extension\Core\View\ChoiceView;
*/
class ChoiceList implements ChoiceListInterface
{
- /**
- * Strategy creating new indices/values by creating a copy of the choice.
- *
- * This strategy can only be used for index creation if choices are
- * guaranteed to only contain ASCII letters, digits and underscores.
- *
- * It can be used for value creation if choices can safely be cast into
- * a (unique) string.
- *
- * @var integer
- */
- const COPY_CHOICE = 0;
-
- /**
- * Strategy creating new indices/values by generating a new integer.
- *
- * This strategy can always be applied, but leads to loss of information
- * in the HTML source code.
- *
- * @var integer
- */
- const GENERATE = 1;
-
/**
* The choices with their indices as keys.
*
@@ -85,24 +62,6 @@ class ChoiceList implements ChoiceListInterface
*/
private $remainingViews = array();
- /**
- * The strategy used for creating choice indices.
- *
- * @var integer
- * @see COPY_CHOICE
- * @see GENERATE
- */
- private $indexStrategy;
-
- /**
- * The strategy used for creating choice values.
- *
- * @var integer
- * @see COPY_CHOICE
- * @see GENERATE
- */
- private $valueStrategy;
-
/**
* Creates a new choice list.
*
@@ -115,16 +74,9 @@ class ChoiceList implements ChoiceListInterface
* should match the structure of $choices.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
- * @param integer $valueStrategy The strategy used to create choice values.
- * One of COPY_CHOICE and GENERATE.
- * @param integer $indexStrategy The strategy used to create choice indices.
- * One of COPY_CHOICE and GENERATE.
*/
- public function __construct($choices, array $labels, array $preferredChoices = array(), $valueStrategy = self::GENERATE, $indexStrategy = self::GENERATE)
+ public function __construct($choices, array $labels, array $preferredChoices = array())
{
- $this->valueStrategy = $valueStrategy;
- $this->indexStrategy = $indexStrategy;
-
$this->initialize($choices, $labels, $preferredChoices);
}
@@ -191,13 +143,6 @@ class ChoiceList implements ChoiceListInterface
public function getChoicesForValues(array $values)
{
$values = $this->fixValues($values);
-
- // If the values are identical to the choices, we can just return them
- // to improve performance a little bit
- if (self::COPY_CHOICE === $this->valueStrategy) {
- return $this->fixChoices(array_intersect($values, $this->values));
- }
-
$choices = array();
foreach ($values as $j => $givenValue) {
@@ -222,13 +167,6 @@ class ChoiceList implements ChoiceListInterface
public function getValuesForChoices(array $choices)
{
$choices = $this->fixChoices($choices);
-
- // If the values are identical to the choices, we can just return them
- // to improve performance a little bit
- if (self::COPY_CHOICE === $this->valueStrategy) {
- return $this->fixValues(array_intersect($choices, $this->choices));
- }
-
$values = array();
foreach ($this->choices as $i => $choice) {
@@ -398,17 +336,15 @@ class ChoiceList implements ChoiceListInterface
$index = $this->createIndex($choice);
if ('' === $index || null === $index || !Form::isValidName((string)$index)) {
- throw new InvalidConfigurationException('The choice list index "' . $index . '" is invalid. Please set the choice field option "index_generation" to ChoiceList::GENERATE.');
+ throw new InvalidConfigurationException('The index "' . $index . '" created by the choice list is invalid. It should be a valid, non-empty Form name.');
}
$value = $this->createValue($choice);
- if (!is_scalar($value)) {
- throw new InvalidConfigurationException('The choice list value of type "' . gettype($value) . '" should be a scalar. Please set the choice field option "value_generation" to ChoiceList::GENERATE.');
+ if (!is_string($value)) {
+ throw new InvalidConfigurationException('The value created by the choice list is of type "' . gettype($value) . '", but should be a string.');
}
- // Always store values as strings to facilitate comparisons
- $value = $this->fixValue($value);
$view = new ChoiceView($value, $label);
$this->choices[$index] = $this->fixChoice($choice);
@@ -448,29 +384,23 @@ class ChoiceList implements ChoiceListInterface
*/
protected function createIndex($choice)
{
- if (self::COPY_CHOICE === $this->indexStrategy) {
- return $choice;
- }
-
return count($this->choices);
}
/**
* Creates a new unique value for this choice.
*
- * Extension point to change the value strategy.
+ * By default, an integer is generated since it cannot be guaranteed that
+ * all values in the list are convertible to (unique) strings. Subclasses
+ * can override this behaviour if they can guarantee this property.
*
* @param mixed $choice The choice to create a value for
*
- * @return integer|string A unique value without character limitations.
+ * @return string A unique string.
*/
protected function createValue($choice)
{
- if (self::COPY_CHOICE === $this->valueStrategy) {
- return $choice;
- }
-
- return count($this->values);
+ return (string) count($this->values);
}
/**
diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php
index 122b50e7e5..b4ce590d28 100644
--- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php
+++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php
@@ -14,17 +14,12 @@ namespace Symfony\Component\Form\Extension\Core\ChoiceList;
/**
* Contains choices that can be selected in a form field.
*
- * Each choice has four different properties:
+ * Each choice has three different properties:
*
* - Choice: The choice that should be returned to the application by the
* choice field. Can be any scalar value or an object, but no
* array.
* - Label: A text representing the choice that is displayed to the user.
- * - Index: A uniquely identifying index that should only contain ASCII
- * characters, digits and underscores. This index is used to
- * identify the choice in the HTML "id" and "name" attributes.
- * It is also used as index of the arrays returned by the various
- * getters of this class.
* - Value: A uniquely identifying value that can contain arbitrary
* characters, but no arrays or objects. This value is displayed
* in the HTML "value" attribute.
diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php
index daf13efcd3..25038cbf82 100644
--- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php
+++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php
@@ -19,8 +19,8 @@ use Symfony\Component\Form\Exception\InvalidPropertyException;
/**
* A choice list for object choices.
*
- * Supports generation of choice labels, choice groups, choice values and
- * choice indices by calling getters of the object (or associated objects).
+ * Supports generation of choice labels, choice groups and choice values
+ * by calling getters of the object (or associated objects).
*
*
* $choices = array($user1, $user2);
@@ -54,13 +54,6 @@ class ObjectChoiceList extends ChoiceList
*/
private $valuePath;
- /**
- * The property path used to obtain the choice index.
- *
- * @var PropertyPath
- */
- private $indexPath;
-
/**
* Creates a new object choice list.
*
@@ -82,18 +75,14 @@ class ObjectChoiceList extends ChoiceList
* @param string $valuePath A property path pointing to the property used
* for the choice values. If not given, integers
* are generated instead.
- * @param string $indexPath A property path pointing to the property used
- * for the choice indices. If not given, integers
- * are generated instead.
*/
- public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, $indexPath = null)
+ public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null)
{
$this->labelPath = $labelPath ? new PropertyPath($labelPath) : null;
$this->groupPath = $groupPath ? new PropertyPath($groupPath) : null;
$this->valuePath = $valuePath ? new PropertyPath($valuePath) : null;
- $this->indexPath = $indexPath ? new PropertyPath($indexPath) : null;
- parent::__construct($choices, array(), $preferredChoices, self::GENERATE, self::GENERATE);
+ parent::__construct($choices, array(), $preferredChoices);
}
/**
@@ -148,27 +137,6 @@ class ObjectChoiceList extends ChoiceList
parent::initialize($choices, $labels, $preferredChoices);
}
- /**
- * Creates a new unique index for this choice.
- *
- * If a property path for the index was given at object creation,
- * the getter behind that path is now called to obtain a new value.
- *
- * Otherwise a new integer is generated.
- *
- * @param mixed $choice The choice to create an index for
- * @return integer|string A unique index containing only ASCII letters,
- * digits and underscores.
- */
- protected function createIndex($choice)
- {
- if ($this->indexPath) {
- return $this->indexPath->getValue($choice);
- }
-
- return parent::createIndex($choice);
- }
-
/**
* Creates a new unique value for this choice.
*
@@ -183,7 +151,7 @@ class ObjectChoiceList extends ChoiceList
protected function createValue($choice)
{
if ($this->valuePath) {
- return $this->valuePath->getValue($choice);
+ return (string) $this->valuePath->getValue($choice);
}
return parent::createValue($choice);
diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php
index 27a7382c03..0ebdcc737b 100644
--- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php
+++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php
@@ -28,27 +28,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* ));
*
*
- * The default value generation strategy is `ChoiceList::COPY_CHOICE`, because
- * choice values must be scalar, and the choices passed to this choice list
- * are guaranteed to be scalar.
- *
- * The default index generation strategy is `ChoiceList::GENERATE`, so that
- * your choices can also contain values that are illegal as indices. If your
- * choices are guaranteed to start with a letter, digit or underscore and only
- * contain letters, digits, underscores, hyphens and colons, you can set the
- * strategy to `ChoiceList::COPY_CHOICE` instead.
- *
- *
- * $choices = array(
- * 'creditcard' => 'Credit card payment',
- * 'cash' => 'Cash payment',
- * );
- *
- * // value generation: COPY_CHOICE (the default)
- * // index generation: COPY_CHOICE (custom)
- * $choiceList = new SimpleChoiceList($choices, array(), ChoiceList::COPY_CHOICE, ChoiceList::COPY_CHOICE);
- *
- *
* @author Bernhard Schussek
*/
class SimpleChoiceList extends ChoiceList
@@ -64,15 +43,35 @@ class SimpleChoiceList extends ChoiceList
* key pointing to the nested array.
* @param array $preferredChoices A flat array of choices that should be
* presented to the user with priority.
- * @param integer $valueStrategy The strategy used to create choice values.
- * One of COPY_CHOICE and GENERATE.
- * @param integer $indexStrategy The strategy used to create choice indices.
- * One of COPY_CHOICE and GENERATE.
*/
- public function __construct(array $choices, array $preferredChoices = array(), $valueStrategy = self::COPY_CHOICE, $indexStrategy = self::GENERATE)
+ public function __construct(array $choices, array $preferredChoices = array())
{
// Flip preferred choices to speed up lookup
- parent::__construct($choices, $choices, array_flip($preferredChoices), $valueStrategy, $indexStrategy);
+ parent::__construct($choices, $choices, array_flip($preferredChoices));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getChoicesForValues(array $values)
+ {
+ $values = $this->fixValues($values);
+
+ // The values are identical to the choices, so we can just return them
+ // to improve performance a little bit
+ return $this->fixChoices(array_intersect($values, $this->getValues()));
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getValuesForChoices(array $choices)
+ {
+ $choices = $this->fixChoices($choices);
+
+ // The choices are identical to the values, so we can just return them
+ // to improve performance a little bit
+ return $this->fixValues(array_intersect($choices, $this->getValues()));
}
/**
@@ -147,16 +146,21 @@ class SimpleChoiceList extends ChoiceList
return $this->fixIndex($choice);
}
-
/**
- * Converts the choices to valid PHP array keys.
- *
- * @param array $choices The choices.
- *
- * @return array Valid PHP array keys.
+ * {@inheritdoc}
*/
protected function fixChoices(array $choices)
{
return $this->fixIndices($choices);
}
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function createValue($choice)
+ {
+ // Choices are guaranteed to be unique and scalar, so we can simply
+ // convert them to strings
+ return (string) $choice;
+ }
}
diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php
index 4211cd6eed..3f15fae91d 100644
--- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php
+++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php
@@ -22,6 +22,22 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
*/
class BooleanToStringTransformer implements DataTransformerInterface
{
+ /**
+ * The value emitted upon transform if the input is true
+ * @var string
+ */
+ private $trueValue;
+
+ /**
+ * Sets the value emitted upon transform if the input is true.
+ *
+ * @param string $trueValue
+ */
+ public function __construct($trueValue)
+ {
+ $this->trueValue = $trueValue;
+ }
+
/**
* Transforms a Boolean into a string.
*
@@ -34,14 +50,14 @@ class BooleanToStringTransformer implements DataTransformerInterface
public function transform($value)
{
if (null === $value) {
- return '';
+ return null;
}
if (!is_bool($value)) {
throw new UnexpectedTypeException($value, 'Boolean');
}
- return true === $value ? '1' : '';
+ return true === $value ? $this->trueValue : null;
}
/**
@@ -63,7 +79,7 @@ class BooleanToStringTransformer implements DataTransformerInterface
throw new UnexpectedTypeException($value, 'string');
}
- return '' !== $value;
+ return true;
}
}
diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php
new file mode 100644
index 0000000000..f7302e33dc
--- /dev/null
+++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php
@@ -0,0 +1,51 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Extension\Core\EventListener;
+
+use Symfony\Component\Form\FormEvents;
+use Symfony\Component\Form\Event\FilterDataEvent;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
+
+/**
+ * Takes care of converting the input from a list of checkboxes to a correctly
+ * indexed array.
+ *
+ * @author Bernhard Schussek
+ */
+class FixCheckboxInputListener implements EventSubscriberInterface
+{
+ private $choiceList;
+
+ /**
+ * Constructor.
+ *
+ * @param ChoiceListInterface $choiceList
+ */
+ public function __construct(ChoiceListInterface $choiceList)
+ {
+ $this->choiceList = $choiceList;
+ }
+
+ public function onBindClientData(FilterDataEvent $event)
+ {
+ $values = (array) $event->getData();
+ $indices = $this->choiceList->getIndicesForValues($values);
+
+ $event->setData(count($indices) > 0 ? array_combine($indices, $values) : array());
+ }
+
+ static public function getSubscribedEvents()
+ {
+ return array(FormEvents::BIND_CLIENT_DATA => 'onBindClientData');
+ }
+}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php b/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php
index 2ac3a0ffad..ddbf8ae897 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php
@@ -25,7 +25,7 @@ class CheckboxType extends AbstractType
public function buildForm(FormBuilder $builder, array $options)
{
$builder
- ->appendClientTransformer(new BooleanToStringTransformer())
+ ->appendClientTransformer(new BooleanToStringTransformer($options['value']))
->setAttribute('value', $options['value'])
;
}
@@ -37,7 +37,7 @@ class CheckboxType extends AbstractType
{
$view
->set('value', $form->getAttribute('value'))
- ->set('checked', (Boolean) $form->getClientData())
+ ->set('checked', null !== $form->getClientData())
;
}
@@ -48,6 +48,9 @@ class CheckboxType extends AbstractType
{
return array(
'value' => '1',
+ 'empty_data' => function (FormInterface $form, $clientData) {
+ return $clientData;
+ },
);
}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
index 03beff4d31..b79d85347a 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
@@ -14,13 +14,14 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener;
+use Symfony\Component\Form\Extension\Core\EventListener\FixCheckboxInputListener;
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
-use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToBooleanArrayTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
@@ -44,9 +45,7 @@ class ChoiceType extends AbstractType
if (!$options['choice_list']) {
$options['choice_list'] = new SimpleChoiceList(
$options['choices'],
- $options['preferred_choices'],
- $options['value_strategy'],
- $options['index_strategy']
+ $options['preferred_choices']
);
}
@@ -81,7 +80,10 @@ class ChoiceType extends AbstractType
if ($options['expanded']) {
if ($options['multiple']) {
- $builder->appendClientTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list']));
+ $builder
+ ->appendClientTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list']))
+ ->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10)
+ ;
} else {
$builder
->appendClientTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list']))
@@ -141,6 +143,26 @@ class ChoiceType extends AbstractType
}
}
+ /**
+ * {@inheritdoc}
+ */
+ public function buildViewBottomUp(FormView $view, FormInterface $form)
+ {
+ if ($view->get('expanded')) {
+ // Radio buttons should have the same name as the parent
+ $childName = $view->get('full_name');
+
+ // Checkboxes should append "[]" to allow multiple selection
+ if ($view->get('multiple')) {
+ $childName .= '[]';
+ }
+
+ foreach ($view->getChildren() as $childView) {
+ $childView->set('full_name', $childName);
+ }
+ }
+ }
+
/**
* {@inheritdoc}
*/
@@ -155,8 +177,6 @@ class ChoiceType extends AbstractType
'choice_list' => null,
'choices' => null,
'preferred_choices' => array(),
- 'value_strategy' => ChoiceList::GENERATE,
- 'index_strategy' => ChoiceList::GENERATE,
'empty_data' => $multiple || $expanded ? array() : '',
'empty_value' => $multiple || $expanded || !isset($options['empty_value']) ? null : '',
'error_bubbling' => false,
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php b/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
index f800678d87..b3e1e43d29 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
@@ -24,8 +24,6 @@ class CountryType extends AbstractType
{
return array(
'choices' => Locale::getDisplayCountries(\Locale::getDefault()),
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
);
}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
index e686b362dc..b634e5f72a 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php
@@ -89,20 +89,14 @@ class DateType extends AbstractType
// Only pass a subset of the options to children
$yearOptions = array(
'choices' => $years,
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['year'],
);
$monthOptions = array(
'choices' => $this->formatMonths($formatter, $months),
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['month'],
);
$dayOptions = array(
'choices' => $days,
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['day'],
);
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php b/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
index da2e315c13..7d722a13e5 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php
@@ -24,7 +24,6 @@ class LanguageType extends AbstractType
{
return array(
'choices' => Locale::getDisplayLanguages(\Locale::getDefault()),
- 'value_strategy' => ChoiceList::COPY_CHOICE,
);
}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php b/src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
index c4841e3184..66e5da9ed2 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
@@ -24,7 +24,6 @@ class LocaleType extends AbstractType
{
return array(
'choices' => Locale::getDisplayLocales(\Locale::getDefault()),
- 'value_strategy' => ChoiceList::COPY_CHOICE,
);
}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php b/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php
index 2500e66c38..fef673e520 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php
@@ -17,16 +17,6 @@ use Symfony\Component\Form\FormView;
class RadioType extends AbstractType
{
- /**
- * {@inheritdoc}
- */
- public function buildView(FormView $view, FormInterface $form)
- {
- if ($view->hasParent()) {
- $view->set('full_name', $view->getParent()->get('full_name'));
- }
- }
-
/**
* {@inheritdoc}
*/
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
index 8ddb180f51..e730f9eb82 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
@@ -59,14 +59,10 @@ class TimeType extends AbstractType
// Only pass a subset of the options to children
$hourOptions = array(
'choices' => $hours,
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['hour'],
);
$minuteOptions = array(
'choices' => $minutes,
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['minute'],
);
@@ -79,8 +75,6 @@ class TimeType extends AbstractType
$secondOptions = array(
'choices' => $seconds,
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- 'index_strategy' => ChoiceList::COPY_CHOICE,
'empty_value' => $options['empty_value']['second'],
);
}
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
index 8d1f74b3eb..c143562a44 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
@@ -27,9 +27,7 @@ class TimezoneType extends AbstractType
*/
public function getDefaultOptions(array $options)
{
- $defaultOptions = array(
- 'value_strategy' => ChoiceList::COPY_CHOICE,
- );
+ $defaultOptions = array();
if (empty($options['choice_list']) && empty($options['choices'])) {
$defaultOptions['choices'] = self::getTimezones();
diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php
index 0e68b88c01..b222c5ef3a 100644
--- a/src/Symfony/Component/Form/Form.php
+++ b/src/Symfony/Component/Form/Form.php
@@ -470,7 +470,11 @@ class Form implements \IteratorAggregate, FormInterface
return $this;
}
- if (is_scalar($clientData) || null === $clientData) {
+ // Don't convert NULL to a string here in order to determine later
+ // whether an empty value has been submitted or whether no value has
+ // been submitted at all. This is important for processing checkboxes
+ // and radio buttons with empty values.
+ if (is_scalar($clientData)) {
$clientData = (string) $clientData;
}
@@ -522,11 +526,13 @@ class Form implements \IteratorAggregate, FormInterface
}
if (null === $clientData || '' === $clientData) {
- $clientData = $this->emptyData;
+ $emptyData = $this->emptyData;
- if ($clientData instanceof \Closure) {
- $clientData = $clientData($this);
+ if ($emptyData instanceof \Closure) {
+ $emptyData = $emptyData($this, $clientData);
}
+
+ $clientData = $emptyData;
}
// Merge form data from children into existing client data
diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
index 4f157e5c9f..32aabb86a6 100644
--- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
@@ -330,8 +330,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name"]
[@required="required"]
[
- ./option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
@@ -352,9 +352,9 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name"]
[@required="required"]
[
- ./option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
/following-sibling::option[@disabled="disabled"][not(@selected)][.="-- sep --"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
]
[count(./option)=3]
'
@@ -376,8 +376,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name"]
[@required="required"]
[
- ./option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
]
[count(./option)=2]
'
@@ -398,9 +398,9 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name"]
[@required="required"]
[
- ./option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
/following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
]
[count(./option)=3]
'
@@ -438,8 +438,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[not(@required)]
[
./option[@value=""][.="[trans][/trans]"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=3]
'
@@ -461,8 +461,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[not(@required)]
[
./option[@value=""][.="[trans][/trans]"]
- /following-sibling::option[@value="0"][not(@selected)][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][not(@selected)][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=3]
'
@@ -485,8 +485,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[not(@required)]
[
./option[@value=""][not(@selected)][.="[trans]Select&Anything&Not&Me[/trans]"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=3]
'
@@ -509,8 +509,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@required="required"]
[
./option[@value=""][.="[trans]Test&Me[/trans]"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=3]
'
@@ -532,8 +532,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@required="required"]
[
./option[@value=""][.="[trans][/trans]"]
- /following-sibling::option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=3]
'
@@ -556,13 +556,13 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name"]
[./optgroup[@label="[trans]Group&1[/trans]"]
[
- ./option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
]
[./optgroup[@label="[trans]Group&2[/trans]"]
- [./option[@value="2"][not(@selected)][.="[trans]Choice&C[/trans]"]]
+ [./option[@value="&c"][not(@selected)][.="[trans]Choice&C[/trans]"]]
[count(./option)=1]
]
[count(./optgroup)=2]
@@ -583,8 +583,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name[]"]
[@multiple="multiple"]
[
- ./option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
@@ -605,8 +605,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name[]"]
[@multiple="multiple"]
[
- ./option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
@@ -627,8 +627,8 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
[@name="name[]"]
[@multiple="multiple"]
[
- ./option[@value="0"][@selected="selected"][.="[trans]Choice&A[/trans]"]
- /following-sibling::option[@value="1"][not(@selected)][.="[trans]Choice&B[/trans]"]
+ ./option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+ /following-sibling::option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
]
[count(./option)=2]
'
@@ -646,9 +646,9 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
- ./input[@type="radio"][@name="name"][@id="name_0"][@value="0"][@checked]
+ ./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="1"][not(@checked)]
+ /following-sibling::input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)]
/following-sibling::label[@for="name_1"][.="[trans]Choice&B[/trans]"]
]
[count(./input)=2]
@@ -711,11 +711,11 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
$this->assertWidgetMatchesXpath($form->createView(), array(),
'/div
[
- ./input[@type="checkbox"][@name="name[0]"][@id="name_0"][@checked][not(@required)]
+ ./input[@type="checkbox"][@name="name[]"][@id="name_0"][@checked][not(@required)]
/following-sibling::label[@for="name_0"][.="[trans]Choice&A[/trans]"]
- /following-sibling::input[@type="checkbox"][@name="name[1]"][@id="name_1"][not(@checked)][not(@required)]
+ /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_1"][not(@checked)][not(@required)]
/following-sibling::label[@for="name_1"][.="[trans]Choice&B[/trans]"]
- /following-sibling::input[@type="checkbox"][@name="name[2]"][@id="name_2"][@checked][not(@required)]
+ /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_2"][@checked][not(@required)]
/following-sibling::label[@for="name_2"][.="[trans]Choice&C[/trans]"]
]
[count(./input)=3]
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php
index d176b9f6f1..8dc382881c 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php
@@ -87,34 +87,6 @@ class ChoiceListTest extends \PHPUnit_Framework_TestCase
), $this->list->getRemainingViews());
}
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
- */
- public function testInitIndexCopyChoiceWithInvalidIndex()
- {
- new ChoiceList(
- array('a.'),
- array('A'),
- array(),
- ChoiceList::GENERATE,
- ChoiceList::COPY_CHOICE
- );
- }
-
- /**
- * @expectedException Symfony\Component\Form\Exception\InvalidConfigurationException
- */
- public function testInitValueCopyChoiceWithInvalidValue()
- {
- new ChoiceList(
- array($this->obj1),
- array('A'),
- array(),
- ChoiceList::COPY_CHOICE,
- ChoiceList::GENERATE
- );
- }
-
public function testGetIndicesForChoices()
{
$choices = array($this->obj2, $this->obj3);
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php
index 0834578ac8..44c1c8f868 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php
@@ -176,28 +176,6 @@ class ObjectChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(0 => new ChoiceView('10', 'A'), 3 => new ChoiceView('40', 'D')), $this->list->getRemainingViews());
}
- public function testInitArrayWithIndexPath()
- {
- $this->obj1 = (object) array('name' => 'A', 'id' => 10);
- $this->obj2 = (object) array('name' => 'B', 'id' => 20);
- $this->obj3 = (object) array('name' => 'C', 'id' => 30);
- $this->obj4 = (object) array('name' => 'D', 'id' => 40);
-
- $this->list = new ObjectChoiceList(
- array($this->obj1, $this->obj2, $this->obj3, $this->obj4),
- 'name',
- array($this->obj2, $this->obj3),
- null,
- null,
- 'id'
- );
-
- $this->assertSame(array(10 => $this->obj1, 20 => $this->obj2, 30 => $this->obj3, 40 => $this->obj4), $this->list->getChoices());
- $this->assertSame(array(10 => '0', 20 => '1', 30 => '2', 40 => '3'), $this->list->getValues());
- $this->assertEquals(array(20 => new ChoiceView('1', 'B'), 30 => new ChoiceView('2', 'C')), $this->list->getPreferredViews());
- $this->assertEquals(array(10 => new ChoiceView('0', 'A'), 40 => new ChoiceView('3', 'D')), $this->list->getRemainingViews());
- }
-
public function testInitArrayUsesToString()
{
$this->obj1 = new ObjectChoiceListTest_EntityWithToString('A');
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php
index 02a3b614a8..485923c9ee 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php
@@ -34,10 +34,10 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
'Group 2' => array(2 => 'C', 3 => 'D'),
);
- $this->list = new SimpleChoiceList($choices, array('b', 'c'), ChoiceList::GENERATE, ChoiceList::GENERATE);
+ $this->list = new SimpleChoiceList($choices, array('b', 'c'));
// Use COPY_CHOICE strategy to test for the various associated problems
- $this->numericList = new SimpleChoiceList($numericChoices, array(1, 2), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE);
+ $this->numericList = new SimpleChoiceList($numericChoices, array(1, 2));
}
protected function tearDown()
@@ -51,18 +51,7 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testInitArray()
{
$choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
- $this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::GENERATE, ChoiceList::GENERATE);
-
- $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices());
- $this->assertSame(array(0 => '0', 1 => '1', 2 => '2'), $this->list->getValues());
- $this->assertEquals(array(1 => new ChoiceView('1', 'B')), $this->list->getPreferredViews());
- $this->assertEquals(array(0 => new ChoiceView('0', 'A'), 2 => new ChoiceView('2', 'C')), $this->list->getRemainingViews());
- }
-
- public function testInitArrayValueCopyChoice()
- {
- $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
- $this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE);
+ $this->list = new SimpleChoiceList($choices, array('b'));
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getChoices());
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c'), $this->list->getValues());
@@ -70,28 +59,17 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(0 => new ChoiceView('a', 'A'), 2 => new ChoiceView('c', 'C')), $this->list->getRemainingViews());
}
- public function testInitArrayIndexCopyChoice()
- {
- $choices = array('a' => 'A', 'b' => 'B', 'c' => 'C');
- $this->list = new SimpleChoiceList($choices, array('b'), ChoiceList::GENERATE, ChoiceList::COPY_CHOICE);
-
- $this->assertSame(array('a' => 'a', 'b' => 'b', 'c' => 'c'), $this->list->getChoices());
- $this->assertSame(array('a' => '0', 'b' => '1', 'c' => '2'), $this->list->getValues());
- $this->assertEquals(array('b' => new ChoiceView('1', 'B')), $this->list->getPreferredViews());
- $this->assertEquals(array('a' => new ChoiceView('0', 'A'), 'c' => new ChoiceView('2', 'C')), $this->list->getRemainingViews());
- }
-
public function testInitNestedArray()
{
$this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getChoices());
- $this->assertSame(array(0 => '0', 1 => '1', 2 => '2', 3 => '3'), $this->list->getValues());
+ $this->assertSame(array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd'), $this->list->getValues());
$this->assertEquals(array(
- 'Group 1' => array(1 => new ChoiceView('1', 'B')),
- 'Group 2' => array(2 => new ChoiceView('2', 'C'))
+ 'Group 1' => array(1 => new ChoiceView('b', 'B')),
+ 'Group 2' => array(2 => new ChoiceView('c', 'C'))
), $this->list->getPreferredViews());
$this->assertEquals(array(
- 'Group 1' => array(0 => new ChoiceView('0', 'A')),
- 'Group 2' => array(3 => new ChoiceView('3', 'D'))
+ 'Group 1' => array(0 => new ChoiceView('a', 'A')),
+ 'Group 2' => array(3 => new ChoiceView('d', 'D'))
), $this->list->getRemainingViews());
}
@@ -116,13 +94,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetIndicesForValues()
{
- $values = array('1', '2');
+ $values = array('b', 'c');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
public function testGetIndicesForValuesIgnoresNonExistingValues()
{
- $values = array('1', '2', '100');
+ $values = array('b', 'c', '100');
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
}
@@ -135,13 +113,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetChoicesForValues()
{
- $values = array('1', '2');
+ $values = array('b', 'c');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
public function testGetChoicesForValuesIgnoresNonExistingValues()
{
- $values = array('1', '2', '100');
+ $values = array('b', 'c', '100');
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
}
@@ -155,13 +133,13 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
public function testGetValuesForChoices()
{
$choices = array('b', 'c');
- $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
+ $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesIgnoresNonExistingValues()
{
$choices = array('b', 'c', 'foobar');
- $this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
+ $this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
}
public function testGetValuesForChoicesDealsWithNumericValues()
@@ -187,7 +165,7 @@ class SimpleChoiceListTest extends \PHPUnit_Framework_TestCase
);
// use COPY_CHOICE strategy to test the problems
- $this->list = new SimpleChoiceList($choices, array(), ChoiceList::COPY_CHOICE, ChoiceList::GENERATE);
+ $this->list = new SimpleChoiceList($choices, array());
$this->assertSame(array($value), $this->list->getValuesForChoices(array($choice)));
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php
index 8d5c868b25..cabdf76f2b 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/BooleanToStringTransformerTest.php
@@ -15,11 +15,13 @@ use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransfo
class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
{
+ const TRUE_VALUE = '1';
+
protected $transformer;
protected function setUp()
{
- $this->transformer = new BooleanToStringTransformer();
+ $this->transformer = new BooleanToStringTransformer(self::TRUE_VALUE);
}
protected function tearDown()
@@ -29,9 +31,9 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
public function testTransform()
{
- $this->assertEquals('1', $this->transformer->transform(true));
- $this->assertEquals('', $this->transformer->transform(false));
- $this->assertSame('', $this->transformer->transform(null));
+ $this->assertEquals(self::TRUE_VALUE, $this->transformer->transform(true));
+ $this->assertNull($this->transformer->transform(false));
+ $this->assertNull($this->transformer->transform(null));
}
public function testTransformExpectsBoolean()
@@ -50,9 +52,9 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
public function testReverseTransform()
{
- $this->assertTrue($this->transformer->reverseTransform('1'));
- $this->assertTrue($this->transformer->reverseTransform('0'));
- $this->assertFalse($this->transformer->reverseTransform(''));
+ $this->assertTrue($this->transformer->reverseTransform(self::TRUE_VALUE));
+ $this->assertTrue($this->transformer->reverseTransform('foobar'));
+ $this->assertTrue($this->transformer->reverseTransform(''));
$this->assertFalse($this->transformer->reverseTransform(null));
}
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php
index cc96bd940a..feb5343a07 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php
@@ -32,6 +32,15 @@ class CheckboxTypeTest extends TypeTestCase
$this->assertTrue($view->get('checked'));
}
+ public function testCheckedIfDataTrueWithEmptyValue()
+ {
+ $form = $this->factory->create('checkbox', null, array('value' => ''));
+ $form->setData(true);
+ $view = $form->createView();
+
+ $this->assertTrue($view->get('checked'));
+ }
+
public function testNotCheckedIfDataFalse()
{
$form = $this->factory->create('checkbox');
@@ -41,8 +50,63 @@ class CheckboxTypeTest extends TypeTestCase
$this->assertFalse($view->get('checked'));
}
+ public function testBindWithValueChecked()
+ {
+ $form = $this->factory->create('checkbox', null, array(
+ 'value' => 'foobar',
+ ));
+ $form->bind('foobar');
+
+ $this->assertTrue($form->getData());
+ $this->assertEquals('foobar', $form->getClientData());
+ }
+
+ public function testBindWithRandomValueChecked()
+ {
+ $form = $this->factory->create('checkbox', null, array(
+ 'value' => 'foobar',
+ ));
+ $form->bind('krixikraxi');
+
+ $this->assertTrue($form->getData());
+ $this->assertEquals('foobar', $form->getClientData());
+ }
+
+ public function testBindWithValueUnchecked()
+ {
+ $form = $this->factory->create('checkbox', null, array(
+ 'value' => 'foobar',
+ ));
+ $form->bind(null);
+
+ $this->assertFalse($form->getData());
+ $this->assertNull($form->getClientData());
+ }
+
+ public function testBindWithEmptyValueChecked()
+ {
+ $form = $this->factory->create('checkbox', null, array(
+ 'value' => '',
+ ));
+ $form->bind('');
+
+ $this->assertTrue($form->getData());
+ $this->assertSame('', $form->getClientData());
+ }
+
+ public function testBindWithEmptyValueUnchecked()
+ {
+ $form = $this->factory->create('checkbox', null, array(
+ 'value' => '',
+ ));
+ $form->bind(null);
+
+ $this->assertFalse($form->getData());
+ $this->assertNull($form->getClientData());
+ }
+
/**
- * @dataProvider proviceTransformedData
+ * @dataProvider provideTransformedData
*/
public function testTransformedData($data, $expected)
{
@@ -60,7 +124,7 @@ class CheckboxTypeTest extends TypeTestCase
$form = $this->builder
->create('expedited_shipping', 'checkbox')
- ->prependClientTransformer($transformer)
+ ->prependNormTransformer($transformer)
->getForm();
$form->setData($data);
$view = $form->createView();
@@ -68,7 +132,7 @@ class CheckboxTypeTest extends TypeTestCase
$this->assertEquals($expected, $view->get('checked'));
}
- public function proviceTransformedData()
+ public function provideTransformedData()
{
return array(
array('expedited', true),
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
index 7e5c635398..50d1453dd1 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php
@@ -11,6 +11,8 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
+use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
+
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
@@ -34,14 +36,6 @@ class ChoiceTypeTest extends TypeTestCase
private $objectChoices;
- private $stringButNumericChoices = array(
- '0' => 'Bernhard',
- '1' => 'Fabien',
- '2' => 'Kris',
- '3' => 'Jon',
- '4' => 'Roman',
- );
-
protected $groupedChoices = array(
'Symfony' => array(
'a' => 'Bernhard',
@@ -183,10 +177,10 @@ class ChoiceTypeTest extends TypeTestCase
'choices' => $this->choices,
));
- $form->bind('1');
+ $form->bind('b');
$this->assertEquals('b', $form->getData());
- $this->assertEquals('1', $form->getClientData());
+ $this->assertEquals('b', $form->getClientData());
}
public function testBindSingleNonExpandedObjectChoices()
@@ -220,10 +214,10 @@ class ChoiceTypeTest extends TypeTestCase
'choices' => $this->choices,
));
- $form->bind(array('0', '1'));
+ $form->bind(array('a', 'b'));
$this->assertEquals(array('a', 'b'), $form->getData());
- $this->assertEquals(array('0', '1'), $form->getClientData());
+ $this->assertEquals(array('a', 'b'), $form->getClientData());
}
public function testBindMultipleNonExpandedObjectChoices()
@@ -256,7 +250,7 @@ class ChoiceTypeTest extends TypeTestCase
'choices' => $this->choices,
));
- $form->bind('1');
+ $form->bind('b');
$this->assertSame('b', $form->getData());
$this->assertFalse($form[0]->getData());
@@ -264,11 +258,34 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
- $this->assertSame('', $form[0]->getClientData());
- $this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertNull($form[0]->getClientData());
+ $this->assertSame('b', $form[1]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
+ }
+
+ public function testBindSingleExpandedNothingChecked()
+ {
+ $form = $this->factory->create('choice', null, array(
+ 'multiple' => false,
+ 'expanded' => true,
+ 'choices' => $this->choices,
+ ));
+
+ $form->bind(null);
+
+ $this->assertSame(null, $form->getData());
+ $this->assertFalse($form[0]->getData());
+ $this->assertFalse($form[1]->getData());
+ $this->assertFalse($form[2]->getData());
+ $this->assertFalse($form[3]->getData());
+ $this->assertFalse($form[4]->getData());
+ $this->assertNull($form[0]->getClientData());
+ $this->assertNull($form[1]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
}
public function testBindSingleExpandedWithFalseDoesNotHaveExtraFields()
@@ -292,17 +309,17 @@ class ChoiceTypeTest extends TypeTestCase
'expanded' => true,
'choices' => array(
'' => 'Empty',
- '1' => 'Not empty',
+ 1 => 'Not empty',
),
));
- $form->bind('0');
+ $form->bind('');
$this->assertNull($form->getData());
$this->assertTrue($form[0]->getData());
$this->assertFalse($form[1]->getData());
- $this->assertSame('1', $form[0]->getClientData());
- $this->assertSame('', $form[1]->getClientData());
+ $this->assertSame('', $form[0]->getClientData());
+ $this->assertNull($form[1]->getClientData());
}
public function testBindSingleExpandedObjectChoices()
@@ -329,11 +346,11 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
- $this->assertSame('', $form[0]->getClientData());
- $this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertNull($form[0]->getClientData());
+ $this->assertSame('2', $form[1]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
}
public function testBindSingleExpandedNumericChoices()
@@ -352,34 +369,11 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
- $this->assertSame('', $form[0]->getClientData());
+ $this->assertNull($form[0]->getClientData());
$this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
- }
-
- public function testBindSingleExpandedStringsButNumericChoices()
- {
- $form = $this->factory->create('choice', null, array(
- 'multiple' => false,
- 'expanded' => true,
- 'choices' => $this->stringButNumericChoices,
- ));
-
- $form->bind('1');
-
- $this->assertSame(1, $form->getData());
- $this->assertFalse($form[0]->getData());
- $this->assertTrue($form[1]->getData());
- $this->assertFalse($form[2]->getData());
- $this->assertFalse($form[3]->getData());
- $this->assertFalse($form[4]->getData());
- $this->assertSame('', $form[0]->getClientData());
- $this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
}
public function testBindMultipleExpanded()
@@ -390,19 +384,65 @@ class ChoiceTypeTest extends TypeTestCase
'choices' => $this->choices,
));
- $form->bind(array(0 => 'a', 1 => 'b'));
+ $form->bind(array('a', 'c'));
- $this->assertSame(array(0 => 'a', 1 => 'b'), $form->getData());
+ $this->assertSame(array('a', 'c'), $form->getData());
$this->assertTrue($form[0]->getData());
- $this->assertTrue($form[1]->getData());
+ $this->assertFalse($form[1]->getData());
+ $this->assertTrue($form[2]->getData());
+ $this->assertFalse($form[3]->getData());
+ $this->assertFalse($form[4]->getData());
+ $this->assertSame('a', $form[0]->getClientData());
+ $this->assertNull($form[1]->getClientData());
+ $this->assertSame('c', $form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
+ }
+
+ public function testBindMultipleExpandedEmpty()
+ {
+ $form = $this->factory->create('choice', null, array(
+ 'multiple' => true,
+ 'expanded' => true,
+ 'choices' => $this->choices,
+ ));
+
+ $form->bind(array());
+
+ $this->assertSame(array(), $form->getData());
+ $this->assertFalse($form[0]->getData());
+ $this->assertFalse($form[1]->getData());
$this->assertFalse($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
- $this->assertSame('1', $form[0]->getClientData());
- $this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertNull($form[0]->getClientData());
+ $this->assertNull($form[1]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
+ }
+
+ public function testBindMultipleExpandedWithEmptyField()
+ {
+ $form = $this->factory->create('choice', null, array(
+ 'multiple' => true,
+ 'expanded' => true,
+ 'choices' => array(
+ '' => 'Empty',
+ 1 => 'Not Empty',
+ 2 => 'Not Empty 2',
+ )
+ ));
+
+ $form->bind(array('', '2'));
+
+ $this->assertSame(array('', 2), $form->getData());
+ $this->assertTrue($form[0]->getData());
+ $this->assertFalse($form[1]->getData());
+ $this->assertTrue($form[2]->getData());
+ $this->assertSame('', $form[0]->getClientData());
+ $this->assertNull($form[1]->getClientData());
+ $this->assertSame('2', $form[2]->getClientData());
}
public function testBindMultipleExpandedObjectChoices()
@@ -421,7 +461,7 @@ class ChoiceTypeTest extends TypeTestCase
),
));
- $form->bind(array(0 => '1', 1 => '2'));
+ $form->bind(array('1', '2'));
$this->assertSame(array($this->objectChoices[0], $this->objectChoices[1]), $form->getData());
$this->assertTrue($form[0]->getData());
@@ -430,10 +470,10 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
$this->assertSame('1', $form[0]->getClientData());
- $this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertSame('2', $form[1]->getClientData());
+ $this->assertNull($form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
}
public function testBindMultipleExpandedNumericChoices()
@@ -444,7 +484,7 @@ class ChoiceTypeTest extends TypeTestCase
'choices' => $this->numericChoices,
));
- $form->bind(array(1 => '1', 2 => '2'));
+ $form->bind(array('1', '2'));
$this->assertSame(array(1, 2), $form->getData());
$this->assertFalse($form[0]->getData());
@@ -452,11 +492,11 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertTrue($form[2]->getData());
$this->assertFalse($form[3]->getData());
$this->assertFalse($form[4]->getData());
- $this->assertSame('', $form[0]->getClientData());
+ $this->assertNull($form[0]->getClientData());
$this->assertSame('1', $form[1]->getClientData());
- $this->assertSame('1', $form[2]->getClientData());
- $this->assertSame('', $form[3]->getClientData());
- $this->assertSame('', $form[4]->getClientData());
+ $this->assertSame('2', $form[2]->getClientData());
+ $this->assertNull($form[3]->getClientData());
+ $this->assertNull($form[4]->getClientData());
}
/*
@@ -598,10 +638,10 @@ class ChoiceTypeTest extends TypeTestCase
$view = $form->createView();
$this->assertEquals(array(
- new ChoiceView('0', 'A'),
- new ChoiceView('1', 'B'),
- new ChoiceView('2', 'C'),
- new ChoiceView('3', 'D'),
+ new ChoiceView('a', 'A'),
+ new ChoiceView('b', 'B'),
+ new ChoiceView('c', 'C'),
+ new ChoiceView('d', 'D'),
), $view->get('choices'));
}
@@ -615,12 +655,12 @@ class ChoiceTypeTest extends TypeTestCase
$view = $form->createView();
$this->assertEquals(array(
- 0 => new ChoiceView('0', 'A'),
- 2 => new ChoiceView('2', 'C'),
+ 0 => new ChoiceView('a', 'A'),
+ 2 => new ChoiceView('c', 'C'),
), $view->get('choices'));
$this->assertEquals(array(
- 1 => new ChoiceView('1', 'B'),
- 3 => new ChoiceView('3', 'D'),
+ 1 => new ChoiceView('b', 'B'),
+ 3 => new ChoiceView('d', 'D'),
), $view->get('preferred_choices'));
}
@@ -634,19 +674,19 @@ class ChoiceTypeTest extends TypeTestCase
$this->assertEquals(array(
'Symfony' => array(
- 0 => new ChoiceView('0', 'Bernhard'),
- 2 => new ChoiceView('2', 'Kris'),
+ 0 => new ChoiceView('a', 'Bernhard'),
+ 2 => new ChoiceView('c', 'Kris'),
),
'Doctrine' => array(
- 4 => new ChoiceView('4', 'Roman'),
+ 4 => new ChoiceView('e', 'Roman'),
),
), $view->get('choices'));
$this->assertEquals(array(
'Symfony' => array(
- 1 => new ChoiceView('1', 'Fabien'),
+ 1 => new ChoiceView('b', 'Fabien'),
),
'Doctrine' => array(
- 3 => new ChoiceView('3', 'Jon'),
+ 3 => new ChoiceView('d', 'Jon'),
),
), $view->get('preferred_choices'));
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php
index fdcb88fa8d..a61f48e7af 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php
@@ -23,11 +23,12 @@ class CountryTypeTest extends LocalizedTestCase
$view = $form->createView();
$choices = $view->get('choices');
- $this->assertEquals(new ChoiceView('DE', 'Deutschland'), $choices['DE']);
- $this->assertEquals(new ChoiceView('GB', 'Vereinigtes Königreich'), $choices['GB']);
- $this->assertEquals(new ChoiceView('US', 'Vereinigte Staaten'), $choices['US']);
- $this->assertEquals(new ChoiceView('FR', 'Frankreich'), $choices['FR']);
- $this->assertEquals(new ChoiceView('MY', 'Malaysia'), $choices['MY']);
+ // Don't check objects for identity
+ $this->assertContains(new ChoiceView('DE', 'Deutschland'), $choices, '', false, false);
+ $this->assertContains(new ChoiceView('GB', 'Vereinigtes Königreich'), $choices, '', false, false);
+ $this->assertContains(new ChoiceView('US', 'Vereinigte Staaten'), $choices, '', false, false);
+ $this->assertContains(new ChoiceView('FR', 'Frankreich'), $choices, '', false, false);
+ $this->assertContains(new ChoiceView('MY', 'Malaysia'), $choices, '', false, false);
}
public function testUnknownCountryIsNotIncluded()
@@ -36,6 +37,10 @@ class CountryTypeTest extends LocalizedTestCase
$view = $form->createView();
$choices = $view->get('choices');
- $this->assertArrayNotHasKey('ZZ', $choices);
+ foreach ($choices as $choice) {
+ if ('ZZ' === $choice->getValue()) {
+ $this->fail('Should not contain choice "ZZ"');
+ }
+ }
}
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
index ab168ed43b..e59f4d1124 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php
@@ -325,8 +325,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 2010 => new ChoiceView('2010', '2010'),
- 2011 => new ChoiceView('2011', '2011'),
+ new ChoiceView('2010', '2010'),
+ new ChoiceView('2011', '2011'),
), $view->getChild('year')->get('choices'));
}
@@ -339,8 +339,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('month')->get('choices'));
}
@@ -354,8 +354,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('month')->get('choices'));
}
@@ -369,8 +369,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 1 => new ChoiceView('1', 'Jän'),
- 4 => new ChoiceView('4', 'Apr')
+ new ChoiceView('1', 'Jän'),
+ new ChoiceView('4', 'Apr')
), $view->getChild('month')->get('choices'));
}
@@ -384,8 +384,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 1 => new ChoiceView('1', 'Jänner'),
- 4 => new ChoiceView('4', 'April'),
+ new ChoiceView('1', 'Jänner'),
+ new ChoiceView('4', 'April'),
), $view->getChild('month')->get('choices'));
}
@@ -399,8 +399,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 1 => new ChoiceView('1', 'Jänner'),
- 4 => new ChoiceView('4', 'April'),
+ new ChoiceView('1', 'Jänner'),
+ new ChoiceView('4', 'April'),
), $view->getChild('month')->get('choices'));
}
@@ -413,8 +413,8 @@ class DateTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('day')->get('choices'));
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php
deleted file mode 100644
index 9681fdb66d..0000000000
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php
+++ /dev/null
@@ -1,24 +0,0 @@
-
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form\Tests\Extension\Core\Type;
-
-class RadioTypeTest extends TypeTestCase
-{
- public function testPassParentFullNameToView()
- {
- $parent = $this->factory->createNamed('field', 'parent');
- $parent->add($this->factory->createNamed('radio', 'child'));
- $view = $parent->createView();
-
- $this->assertEquals('parent', $view['child']->get('full_name'));
- }
-}
diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php
index 6ef5ef3e68..ce6718666b 100644
--- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php
@@ -243,8 +243,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('hour')->get('choices'));
}
@@ -257,8 +257,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('minute')->get('choices'));
}
@@ -272,8 +272,8 @@ class TimeTypeTest extends LocalizedTestCase
$view = $form->createView();
$this->assertEquals(array(
- 6 => new ChoiceView('6', '06'),
- 7 => new ChoiceView('7', '07'),
+ new ChoiceView('6', '06'),
+ new ChoiceView('7', '07'),
), $view->getChild('second')->get('choices'));
}