[Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted
This commit is contained in:
parent
79a214fa75
commit
6283b0e93d
@ -49,15 +49,11 @@ class BooleanToStringTransformer implements DataTransformerInterface
|
||||
*/
|
||||
public function transform($value)
|
||||
{
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!is_bool($value)) {
|
||||
throw new TransformationFailedException('Expected a Boolean.');
|
||||
}
|
||||
|
||||
return true === $value ? $this->trueValue : null;
|
||||
return $value ? $this->trueValue : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace Symfony\Component\Form\Extension\Core\EventListener;
|
||||
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
use Symfony\Component\Form\FormEvents;
|
||||
use Symfony\Component\Form\FormEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
@ -38,10 +39,43 @@ class FixCheckboxInputListener implements EventSubscriberInterface
|
||||
|
||||
public function preSubmit(FormEvent $event)
|
||||
{
|
||||
$values = (array) $event->getData();
|
||||
$indices = $this->choiceList->getIndicesForValues($values);
|
||||
$data = $event->getData();
|
||||
|
||||
$event->setData(count($indices) > 0 ? array_combine($indices, $values) : array());
|
||||
if (is_array($data)) {
|
||||
// Flip the submitted values for faster lookup
|
||||
// It's better to flip this array than $existingValues because
|
||||
// $submittedValues is generally smaller.
|
||||
$submittedValues = array_flip($data);
|
||||
|
||||
// Since expanded choice fields are completely loaded anyway, we
|
||||
// can just as well get the values again without losing performance.
|
||||
$existingValues = $this->choiceList->getValues();
|
||||
|
||||
// Clear the data array and fill it with correct indices
|
||||
$data = array();
|
||||
|
||||
foreach ($existingValues as $index => $value) {
|
||||
if (isset($submittedValues[$value])) {
|
||||
// Value was submitted
|
||||
$data[$index] = $value;
|
||||
unset($submittedValues[$value]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($submittedValues) > 0) {
|
||||
throw new TransformationFailedException(sprintf(
|
||||
'The following choices were not found: "%s"',
|
||||
implode('", "', array_keys($submittedValues))
|
||||
));
|
||||
}
|
||||
} elseif ('' === $data || null === $data) {
|
||||
// Empty values are always accepted.
|
||||
$data = array();
|
||||
}
|
||||
|
||||
// Else leave the data unchanged to provoke an error during submission
|
||||
|
||||
$event->setData($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -42,10 +42,22 @@ class FixRadioInputListener implements EventSubscriberInterface
|
||||
|
||||
public function preSubmit(FormEvent $event)
|
||||
{
|
||||
$value = $event->getData();
|
||||
$index = current($this->choiceList->getIndicesForValues(array($value)));
|
||||
$data = $event->getData();
|
||||
|
||||
$event->setData(false !== $index ? array($index => $value) : ($this->placeholderPresent ? array('placeholder' => '') : array())) ;
|
||||
// Since expanded choice fields are completely loaded anyway, we
|
||||
// can just as well get the values again without losing performance.
|
||||
$existingValues = $this->choiceList->getValues();
|
||||
|
||||
if (false !== ($index = array_search($data, $existingValues, true))) {
|
||||
$data = array($index => $data);
|
||||
} elseif ('' === $data || null === $data) {
|
||||
// Empty values are always accepted.
|
||||
$data = $this->placeholderPresent ? array('placeholder' => '') : array();
|
||||
}
|
||||
|
||||
// Else leave the data unchanged to provoke an error during submission
|
||||
|
||||
$event->setData($data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -25,9 +25,14 @@ class CheckboxType extends AbstractType
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->addViewTransformer(new BooleanToStringTransformer($options['value']))
|
||||
;
|
||||
// Unlike in other types, where the data is NULL by default, it
|
||||
// needs to be a Boolean here. setData(null) is not acceptable
|
||||
// for checkboxes and radio buttons (unless a custom model
|
||||
// transformer handles this case).
|
||||
// We cannot solve this case via overriding the "data" option, because
|
||||
// doing so also calls setDataLocked(true).
|
||||
$builder->setData(isset($options['data']) ? $options['data'] : false);
|
||||
$builder->addViewTransformer(new BooleanToStringTransformer($options['value']));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,8 +51,8 @@ class CheckboxType extends AbstractType
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$emptyData = function (FormInterface $form, $clientData) {
|
||||
return $clientData;
|
||||
$emptyData = function (FormInterface $form, $viewData) {
|
||||
return $viewData;
|
||||
};
|
||||
|
||||
$resolver->setDefaults(array(
|
||||
|
@ -522,6 +522,11 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
|
||||
$dispatcher = $this->config->getEventDispatcher();
|
||||
|
||||
$modelData = null;
|
||||
$normData = null;
|
||||
$viewData = null;
|
||||
|
||||
try {
|
||||
// Hook to change content of the data submitted by the browser
|
||||
if ($dispatcher->hasListeners(FormEvents::PRE_SUBMIT)) {
|
||||
$event = new FormEvent($this, $submittedData);
|
||||
@ -534,12 +539,16 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
// since forms without children may also be compound.
|
||||
// (think of empty collection forms)
|
||||
if ($this->config->getCompound()) {
|
||||
if (!is_array($submittedData)) {
|
||||
if (null === $submittedData) {
|
||||
$submittedData = array();
|
||||
}
|
||||
|
||||
if (!is_array($submittedData)) {
|
||||
throw new TransformationFailedException('Compound forms expect an array or NULL on submission.');
|
||||
}
|
||||
|
||||
foreach ($this->children as $name => $child) {
|
||||
if (array_key_exists($name, $submittedData) || $clearMissing) {
|
||||
if (isset($submittedData[$name]) || $clearMissing) {
|
||||
$child->submit(isset($submittedData[$name]) ? $submittedData[$name] : null, $clearMissing);
|
||||
unset($submittedData[$name]);
|
||||
}
|
||||
@ -554,13 +563,7 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
// changes in the grandchildren (i.e. children of the form that inherits
|
||||
// its parent's data) into account.
|
||||
// (see InheritDataAwareIterator below)
|
||||
if ($this->config->getInheritData()) {
|
||||
$this->submitted = true;
|
||||
|
||||
// When POST_SUBMIT is reached, the data is not yet updated, so pass
|
||||
// NULL to prevent hard-to-debug bugs.
|
||||
$dataForPostSubmit = null;
|
||||
} else {
|
||||
if (!$this->config->getInheritData()) {
|
||||
// If the form is compound, the default data in view format
|
||||
// is reused. The data of the children is merged into this
|
||||
// default data using the data mapper.
|
||||
@ -586,19 +589,15 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
// descendants that inherit this form's data.
|
||||
// These descendants will not be submitted normally (see the check
|
||||
// for $this->config->getInheritData() above)
|
||||
$iterator = new InheritDataAwareIterator($this->children);
|
||||
$iterator = new \RecursiveIteratorIterator($iterator);
|
||||
$this->config->getDataMapper()->mapFormsToData($iterator, $viewData);
|
||||
$childrenIterator = new InheritDataAwareIterator($this->children);
|
||||
$childrenIterator = new \RecursiveIteratorIterator($childrenIterator);
|
||||
$this->config->getDataMapper()->mapFormsToData($childrenIterator, $viewData);
|
||||
}
|
||||
|
||||
$modelData = null;
|
||||
$normData = null;
|
||||
|
||||
try {
|
||||
// Normalize data to unified representation
|
||||
$normData = $this->viewToNorm($viewData);
|
||||
|
||||
// Hook to change content of the data into the normalized
|
||||
// Hook to change content of the data in the normalized
|
||||
// representation
|
||||
if ($dispatcher->hasListeners(FormEvents::SUBMIT)) {
|
||||
$event = new FormEvent($this, $normData);
|
||||
@ -609,8 +608,17 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
// Synchronize representations - must not change the content!
|
||||
$modelData = $this->normToModel($normData);
|
||||
$viewData = $this->normToView($normData);
|
||||
}
|
||||
} catch (TransformationFailedException $e) {
|
||||
$this->synchronized = false;
|
||||
|
||||
// If $viewData was not yet set, set it to $submittedData so that
|
||||
// the erroneous data is accessible on the form.
|
||||
// Forms that inherit data never set any data, because the getters
|
||||
// forward to the parent form's getters anyway.
|
||||
if (null === $viewData && !$this->config->getInheritData()) {
|
||||
$viewData = $submittedData;
|
||||
}
|
||||
}
|
||||
|
||||
$this->submitted = true;
|
||||
@ -618,11 +626,8 @@ class Form implements \IteratorAggregate, FormInterface
|
||||
$this->normData = $normData;
|
||||
$this->viewData = $viewData;
|
||||
|
||||
$dataForPostSubmit = $viewData;
|
||||
}
|
||||
|
||||
if ($dispatcher->hasListeners(FormEvents::POST_SUBMIT)) {
|
||||
$event = new FormEvent($this, $dataForPostSubmit);
|
||||
$event = new FormEvent($this, $viewData);
|
||||
$dispatcher->dispatch(FormEvents::POST_SUBMIT, $event);
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,9 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
const TRUE_VALUE = '1';
|
||||
|
||||
/**
|
||||
* @var BooleanToStringTransformer
|
||||
*/
|
||||
protected $transformer;
|
||||
|
||||
protected function setUp()
|
||||
@ -33,20 +36,29 @@ class BooleanToStringTransformerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertEquals(self::TRUE_VALUE, $this->transformer->transform(true));
|
||||
$this->assertNull($this->transformer->transform(false));
|
||||
$this->assertNull($this->transformer->transform(null));
|
||||
}
|
||||
|
||||
public function testTransformExpectsBoolean()
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testTransformFailsIfNull()
|
||||
{
|
||||
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
|
||||
$this->transformer->transform(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testTransformFailsIfString()
|
||||
{
|
||||
$this->transformer->transform('1');
|
||||
}
|
||||
|
||||
public function testReverseTransformExpectsString()
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformFailsIfInteger()
|
||||
{
|
||||
$this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
|
||||
|
||||
$this->transformer->reverseTransform(1);
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,15 @@ use Symfony\Component\Form\CallbackTransformer;
|
||||
|
||||
class CheckboxTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
{
|
||||
public function testDataIsFalseByDefault()
|
||||
{
|
||||
$form = $this->factory->create('checkbox');
|
||||
|
||||
$this->assertFalse($form->getData());
|
||||
$this->assertFalse($form->getNormData());
|
||||
$this->assertNull($form->getViewData());
|
||||
}
|
||||
|
||||
public function testPassValueToView()
|
||||
{
|
||||
$form = $this->factory->create('checkbox', null, array('value' => 'foobar'));
|
||||
@ -105,58 +114,60 @@ class CheckboxTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form->getViewData());
|
||||
}
|
||||
|
||||
public function testBindWithEmptyValueAndFalseUnchecked()
|
||||
public function testSubmitWithEmptyValueAndFalseUnchecked()
|
||||
{
|
||||
$form = $this->factory->create('checkbox', null, array(
|
||||
'value' => '',
|
||||
));
|
||||
$form->bind(false);
|
||||
$form->submit(false);
|
||||
|
||||
$this->assertFalse($form->getData());
|
||||
$this->assertNull($form->getViewData());
|
||||
}
|
||||
|
||||
public function testBindWithEmptyValueAndTrueChecked()
|
||||
public function testSubmitWithEmptyValueAndTrueChecked()
|
||||
{
|
||||
$form = $this->factory->create('checkbox', null, array(
|
||||
'value' => '',
|
||||
));
|
||||
$form->bind(true);
|
||||
$form->submit(true);
|
||||
|
||||
$this->assertTrue($form->getData());
|
||||
$this->assertSame('', $form->getViewData());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideTransformedData
|
||||
* @dataProvider provideCustomModelTransformerData
|
||||
*/
|
||||
public function testTransformedData($data, $expected)
|
||||
public function testCustomModelTransformer($data, $checked)
|
||||
{
|
||||
// present a binary status field as a checkbox
|
||||
$transformer = new CallbackTransformer(
|
||||
function ($value) {
|
||||
return 'expedited' == $value;
|
||||
return 'checked' == $value;
|
||||
},
|
||||
function ($value) {
|
||||
return $value ? 'expedited' : 'standard';
|
||||
return $value ? 'checked' : 'unchecked';
|
||||
}
|
||||
);
|
||||
|
||||
$form = $this->builder
|
||||
->create('expedited_shipping', 'checkbox')
|
||||
$form = $this->factory->createBuilder('checkbox')
|
||||
->addModelTransformer($transformer)
|
||||
->getForm();
|
||||
|
||||
$form->setData($data);
|
||||
$view = $form->createView();
|
||||
|
||||
$this->assertEquals($expected, $view->vars['checked']);
|
||||
$this->assertSame($data, $form->getData());
|
||||
$this->assertSame($checked, $form->getNormData());
|
||||
$this->assertEquals($checked, $view->vars['checked']);
|
||||
}
|
||||
|
||||
public function provideTransformedData()
|
||||
public function provideCustomModelTransformerData()
|
||||
{
|
||||
return array(
|
||||
array('expedited', true),
|
||||
array('standard', false),
|
||||
array('checked', true),
|
||||
array('unchecked', false),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -231,6 +231,21 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertEquals('b', $form->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleNonExpandedInvalidChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => false,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('foobar');
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertEquals('foobar', $form->getViewData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
}
|
||||
|
||||
public function testSubmitSingleNonExpandedObjectChoices()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
@ -268,6 +283,36 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertEquals(array('a', 'b'), $form->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleNonExpandedInvalidScalarChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => false,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('foobar');
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertEquals('foobar', $form->getViewData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleNonExpandedInvalidArrayChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => false,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit(array('a', 'foobar'));
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertEquals(array('a', 'foobar'), $form->getViewData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleNonExpandedObjectChoices()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
@ -309,6 +354,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
3 => false,
|
||||
4 => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertFalse($form[0]->getData());
|
||||
$this->assertTrue($form[1]->getData());
|
||||
@ -322,6 +369,34 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedRequiredInvalidChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'required' => true,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('foobar');
|
||||
|
||||
$this->assertSame(null, $form->getData());
|
||||
$this->assertSame('foobar', $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedNonRequired()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
@ -342,6 +417,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
4 => false,
|
||||
'placeholder' => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertFalse($form['placeholder']->getData());
|
||||
$this->assertFalse($form[0]->getData());
|
||||
@ -357,7 +434,35 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedRequiredNothingChecked()
|
||||
public function testSubmitSingleExpandedNonRequiredInvalidChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'required' => false,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('foobar');
|
||||
|
||||
$this->assertSame(null, $form->getData());
|
||||
$this->assertSame('foobar', $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedRequiredNull()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
@ -376,6 +481,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
3 => false,
|
||||
4 => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertFalse($form[0]->getData());
|
||||
$this->assertFalse($form[1]->getData());
|
||||
@ -389,7 +496,75 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedNonRequiredNothingChecked()
|
||||
public function testSubmitSingleExpandedRequiredEmpty()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'required' => true,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('');
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame(array(
|
||||
0 => false,
|
||||
1 => false,
|
||||
2 => false,
|
||||
3 => false,
|
||||
4 => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedRequiredFalse()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'required' => true,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit(false);
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame(array(
|
||||
0 => false,
|
||||
1 => false,
|
||||
2 => false,
|
||||
3 => false,
|
||||
4 => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedNonRequiredNull()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
@ -409,6 +584,8 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
4 => false,
|
||||
'placeholder' => true,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertTrue($form['placeholder']->getData());
|
||||
$this->assertFalse($form[0]->getData());
|
||||
@ -424,22 +601,44 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitFalseToSingleExpandedRequiredDoesNotProduceExtraChildrenError()
|
||||
public function testSubmitSingleExpandedNonRequiredEmpty()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'required' => true,
|
||||
'required' => false,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit(false);
|
||||
$form->submit('');
|
||||
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame(array(
|
||||
0 => false,
|
||||
1 => false,
|
||||
2 => false,
|
||||
3 => false,
|
||||
4 => false,
|
||||
'placeholder' => true,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertTrue($form['placeholder']->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('', $form['placeholder']->getViewData());
|
||||
$this->assertNull($form[0]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitFalseToSingleExpandedNonRequiredDoesNotProduceExtraChildrenError()
|
||||
public function testSubmitSingleExpandedNonRequiredFalse()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => false,
|
||||
@ -450,8 +649,30 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
|
||||
$form->submit(false);
|
||||
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame(array(
|
||||
0 => false,
|
||||
1 => false,
|
||||
2 => false,
|
||||
3 => false,
|
||||
4 => false,
|
||||
'placeholder' => true,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertTrue($form['placeholder']->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('', $form['placeholder']->getViewData());
|
||||
$this->assertNull($form[0]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitSingleExpandedWithEmptyChild()
|
||||
@ -539,6 +760,16 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$form->submit(array('a', 'c'));
|
||||
|
||||
$this->assertSame(array('a', 'c'), $form->getData());
|
||||
$this->assertSame(array(
|
||||
0 => true,
|
||||
1 => false,
|
||||
2 => true,
|
||||
3 => false,
|
||||
4 => false,
|
||||
), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
|
||||
$this->assertTrue($form[0]->getData());
|
||||
$this->assertFalse($form[1]->getData());
|
||||
$this->assertTrue($form[2]->getData());
|
||||
@ -551,6 +782,60 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleExpandedInvalidScalarChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit('foobar');
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame('foobar', $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleExpandedInvalidArrayChoice()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
'multiple' => true,
|
||||
'expanded' => true,
|
||||
'choices' => $this->choices,
|
||||
));
|
||||
|
||||
$form->submit(array('a', 'foobar'));
|
||||
|
||||
$this->assertNull($form->getData());
|
||||
$this->assertSame(array('a', 'foobar'), $form->getViewData());
|
||||
$this->assertEmpty($form->getExtraData());
|
||||
$this->assertFalse($form->isSynchronized());
|
||||
|
||||
$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]->getViewData());
|
||||
$this->assertNull($form[1]->getViewData());
|
||||
$this->assertNull($form[2]->getViewData());
|
||||
$this->assertNull($form[3]->getViewData());
|
||||
$this->assertNull($form[4]->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitMultipleExpandedEmpty()
|
||||
{
|
||||
$form = $this->factory->create('choice', null, array(
|
||||
|
Reference in New Issue
Block a user