Skip to content

Commit 14c6ba2

Browse files
Merge branch '3.4' into 4.4
* 3.4: [FrameworkBundle] remove redundant PHPDoc in console Descriptor and subclass [Form] Handle false as empty value on expanded choices
2 parents 2aab7c1 + 1ed27fd commit 14c6ba2

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

Extension/Core/Type/CheckboxType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3333
// doing so also calls setDataLocked(true).
3434
$builder->setData(isset($options['data']) ? $options['data'] : false);
3535
$builder->addViewTransformer(new BooleanToStringTransformer($options['value'], $options['false_values']));
36+
$builder->setAttribute('_false_is_empty', true); // @internal - A boolean flag to treat false as empty, see Form::isEmpty() - Do not rely on it, it will be removed in Symfony 5.1.
3637
}
3738

3839
/**

Form.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ public function isEmpty()
731731
// arrays, countables
732732
((\is_array($this->modelData) || $this->modelData instanceof \Countable) && 0 === \count($this->modelData)) ||
733733
// traversables that are not countable
734-
($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData));
734+
($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData)) ||
735+
// @internal - Do not rely on it, it will be removed in Symfony 5.1.
736+
(false === $this->modelData && $this->config->getAttribute('_false_is_empty'));
735737
}
736738

737739
/**

Tests/Extension/Core/Type/CheckboxTypeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,13 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expect
220220
$this->assertSame($expectedData, $form->getNormData());
221221
$this->assertSame($expectedData, $form->getData());
222222
}
223+
224+
public function testSubmitNullIsEmpty()
225+
{
226+
$form = $this->factory->create(static::TESTED_TYPE);
227+
228+
$form->submit(null);
229+
230+
$this->assertTrue($form->isEmpty());
231+
}
223232
}

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,4 +2049,45 @@ public function provideTrimCases()
20492049
'Multiple expanded' => [true, true],
20502050
];
20512051
}
2052+
2053+
/**
2054+
* @dataProvider expandedIsEmptyWhenNoRealChoiceIsSelectedProvider
2055+
*/
2056+
public function testExpandedIsEmptyWhenNoRealChoiceIsSelected($expected, $submittedData, $multiple, $required, $placeholder)
2057+
{
2058+
$options = [
2059+
'expanded' => true,
2060+
'choices' => [
2061+
'foo' => 'bar',
2062+
],
2063+
'multiple' => $multiple,
2064+
'required' => $required,
2065+
];
2066+
2067+
if (!$multiple) {
2068+
$options['placeholder'] = $placeholder;
2069+
}
2070+
2071+
$form = $this->factory->create(static::TESTED_TYPE, null, $options);
2072+
2073+
$form->submit($submittedData);
2074+
2075+
$this->assertSame($expected, $form->isEmpty());
2076+
}
2077+
2078+
public function expandedIsEmptyWhenNoRealChoiceIsSelectedProvider()
2079+
{
2080+
// Some invalid cases are voluntarily not tested:
2081+
// - multiple with placeholder
2082+
// - required with placeholder
2083+
return [
2084+
'Nothing submitted / single / not required / without a placeholder -> should be empty' => [true, null, false, false, null],
2085+
'Nothing submitted / single / not required / with a placeholder -> should not be empty' => [false, null, false, false, 'ccc'], // It falls back on the placeholder
2086+
'Nothing submitted / single / required / without a placeholder -> should be empty' => [true, null, false, true, null],
2087+
'Nothing submitted / single / required / with a placeholder -> should be empty' => [true, null, false, true, 'ccc'],
2088+
'Nothing submitted / multiple / not required / without a placeholder -> should be empty' => [true, null, true, false, null],
2089+
'Nothing submitted / multiple / required / without a placeholder -> should be empty' => [true, null, true, true, null],
2090+
'Placeholder submitted / single / not required / with a placeholder -> should not be empty' => [false, '', false, false, 'ccc'], // The placeholder is a selected value
2091+
];
2092+
}
20522093
}

0 commit comments

Comments
 (0)