merged branch bschussek/issue3839 (PR #3859)

Commits
-------

61d792e [Form] Changed checkboxes in an expanded multiple-choice field to not include the choice index
bc9bc4a [Form] Fixed behavior of expanded multiple-choice field when submitted without ticks
2e07256 [Form] Simplified choice list API
2645120 [Form] Fixed handling of expanded choice lists, checkboxes and radio buttons with empty values ("")

Discussion
----------

[Form] Fixed handling of empty values in checkbox/radio/choice type

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #3839, #3366
Todo: -

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue3839)

This PR fixes the processing of checkboxes and radio buttons with empty "value" attributes as well as of expanded choice forms with empty values. Additionally, some unnecessary complexity has been removed of the new ChoiceList API.

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

by stof at 2012-04-10T13:56:12Z

You probably need to change some things in the CHANGELOG file too

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

by bschussek at 2012-04-10T14:39:24Z

No. This is an update of a previous post-2.0 PR, the CHANGELOG is still accurate.

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

by stof at 2012-04-10T14:46:47Z

well, doesn't it require changes to the description of previous changes related to the ChoiceList ? It does in the UPGRADE file so it is weird if the CHANGELOG does not require any change.

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

by bschussek at 2012-04-10T14:56:05Z

Feel free to check yourself :)
This commit is contained in:
Fabien Potencier 2012-04-10 20:02:12 +02:00
commit 517f8e0162
32 changed files with 456 additions and 488 deletions

View File

@ -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

View File

@ -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.

View File

@ -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);

View File

@ -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()

View File

@ -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);

View File

@ -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);
}
/**

View File

@ -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.

View File

@ -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).
*
* <code>
* $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);

View File

@ -28,27 +28,6 @@ use Symfony\Component\Form\Exception\UnexpectedTypeException;
* ));
* </code>
*
* 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.
*
* <code>
* $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);
* </code>
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
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;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <bernhard.schussek@symfony-project.com>
*/
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');
}
}

View File

@ -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;
},
);
}

View File

@ -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,

View File

@ -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,
);
}

View File

@ -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'],
);

View File

@ -24,7 +24,6 @@ class LanguageType extends AbstractType
{
return array(
'choices' => Locale::getDisplayLanguages(\Locale::getDefault()),
'value_strategy' => ChoiceList::COPY_CHOICE,
);
}

View File

@ -24,7 +24,6 @@ class LocaleType extends AbstractType
{
return array(
'choices' => Locale::getDisplayLocales(\Locale::getDefault()),
'value_strategy' => ChoiceList::COPY_CHOICE,
);
}

View File

@ -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}
*/

View File

@ -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'],
);
}

View File

@ -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();

View File

@ -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

View File

@ -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]

View File

@ -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);

View File

@ -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');

View File

@ -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)));
}

View File

@ -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));
}
}

View File

@ -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),

View File

@ -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'));
}

View File

@ -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"');
}
}
}
}

View File

@ -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'));
}

View File

@ -1,24 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'));
}
}

View File

@ -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'));
}