Skip to content

Commit 93add72

Browse files
Merge branch '4.4' into 5.0
* 4.4: (34 commits) Add test for tagged iterator with numeric index Fix container lint command when a synthetic service is used in combination with the expression language [Validator][Range] Fix typos [SecurityBundle] Minor fix in LDAP config tree builder [HttpClient] fix requests to hosts that idn_to_ascii() cannot handle [FrameworkBundle] remove redundant PHPDoc in console Descriptor and subclass [Mime] remove phpdoc mentioning Utf8AddressEncoder Add missing phpdoc Remove int return type from FlattenException::getCode [Yaml] fix dumping strings containing CRs [DI] Fix XmlFileLoader bad error message [Form] Handle false as empty value on expanded choices [Messenger] Add ext-redis min version req to tests Tweak message improve PlaintextPasswordEncoder docBlock summary [Validator] Add two missing translations for the Arabic (ar) locale Use some PHP 5.4 constants unconditionally Add new packages on the link script [DI] fix dumping errored definitions [DI] ignore extra tags added by autoconfiguration in PriorityTaggedServiceTrait ...
2 parents 7d3afc4 + 14c6ba2 commit 93add72

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
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
@@ -730,7 +730,9 @@ public function isEmpty()
730730
// arrays, countables
731731
((\is_array($this->modelData) || $this->modelData instanceof \Countable) && 0 === \count($this->modelData)) ||
732732
// traversables that are not countable
733-
($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData));
733+
($this->modelData instanceof \Traversable && 0 === iterator_count($this->modelData)) ||
734+
// @internal - Do not rely on it, it will be removed in Symfony 5.1.
735+
(false === $this->modelData && $this->config->getAttribute('_false_is_empty'));
734736
}
735737

736738
/**

FormRenderer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ public function humanize(string $text)
291291
public function encodeCurrency(Environment $environment, string $text, string $widget = ''): string
292292
{
293293
if ('UTF-8' === $charset = $environment->getCharset()) {
294-
$text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
294+
$text = htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
295295
} else {
296-
$text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
296+
$text = htmlentities($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
297297
$text = iconv('UTF-8', $charset, $text);
298298
$widget = iconv('UTF-8', $charset, $widget);
299299
}

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)