Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d0698b2

Browse filesBrowse files
bug #29249 [Form] Fixed empty data for compound date interval (HeahDude)
This PR was merged into the 3.4 branch. Discussion ---------- [Form] Fixed empty data for compound date interval | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #4715 (continuation) | License | MIT | Doc PR | ~ Following #29182, since this type has been introduced in Symfony 3 but with the same limitation explained in #4715. So for consistency this needs to be fixed here as well. Commits ------- 38a2abc [Form] Fixed empty data for compound date interval
2 parents e166d96 + 38a2abc commit d0698b2
Copy full SHA for d0698b2

File tree

Expand file treeCollapse file tree

2 files changed

+46
-22
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+46
-22
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php
+18-22Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class DateIntervalType extends AbstractType
3838
'seconds',
3939
);
4040
private static $widgets = array(
41-
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
42-
'integer' => 'Symfony\Component\Form\Extension\Core\Type\IntegerType',
43-
'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
41+
'text' => TextType::class,
42+
'integer' => IntegerType::class,
43+
'choice' => ChoiceType::class,
4444
);
4545

4646
/**
@@ -96,31 +96,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9696
if ('single_text' === $options['widget']) {
9797
$builder->addViewTransformer(new DateIntervalToStringTransformer($format));
9898
} else {
99-
$childOptions = array();
10099
foreach ($this->timeParts as $part) {
101100
if ($options['with_'.$part]) {
102-
$childOptions[$part] = array(
101+
$childOptions = array(
103102
'error_bubbling' => true,
104103
'label' => $options['labels'][$part],
104+
// Append generic carry-along options
105+
'required' => $options['required'],
106+
'translation_domain' => $options['translation_domain'],
107+
// when compound the array entries are ignored, we need to cascade the configuration here
108+
'empty_data' => isset($options['empty_data'][$part]) ? $options['empty_data'][$part] : null,
105109
);
106110
if ('choice' === $options['widget']) {
107-
$childOptions[$part]['choice_translation_domain'] = false;
108-
$childOptions[$part]['choices'] = $options[$part];
109-
$childOptions[$part]['placeholder'] = $options['placeholder'][$part];
111+
$childOptions['choice_translation_domain'] = false;
112+
$childOptions['choices'] = $options[$part];
113+
$childOptions['placeholder'] = $options['placeholder'][$part];
110114
}
111-
}
112-
}
113-
// Append generic carry-along options
114-
foreach (array('required', 'translation_domain') as $passOpt) {
115-
foreach ($this->timeParts as $part) {
116-
if ($options['with_'.$part]) {
117-
$childOptions[$part][$passOpt] = $options[$passOpt];
118-
}
119-
}
120-
}
121-
foreach ($this->timeParts as $part) {
122-
if ($options['with_'.$part]) {
123-
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions[$part]);
115+
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions);
124116
if ('integer' === $options['widget']) {
125117
$childForm->addModelTransformer(
126118
new ReversedTransformer(
@@ -132,7 +124,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
132124
}
133125
}
134126
if ($options['with_invert']) {
135-
$builder->add('invert', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
127+
$builder->add('invert', CheckboxType::class, array(
136128
'label' => $options['labels']['invert'],
137129
'error_bubbling' => true,
138130
'required' => false,
@@ -180,6 +172,9 @@ public function configureOptions(OptionsResolver $resolver)
180172
$compound = function (Options $options) {
181173
return 'single_text' !== $options['widget'];
182174
};
175+
$emptyData = function (Options $options) {
176+
return 'single_text' === $options['widget'] ? '' : array();
177+
};
183178

184179
$placeholderDefault = function (Options $options) {
185180
return $options['required'] ? null : '';
@@ -238,6 +233,7 @@ public function configureOptions(OptionsResolver $resolver)
238233
// this option.
239234
'data_class' => null,
240235
'compound' => $compound,
236+
'empty_data' => $emptyData,
241237
'labels' => array(),
242238
)
243239
);

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/DateIntervalTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/DateIntervalTypeTest.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,32 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
423423
$this->assertSame($expectedData, $form->getNormData());
424424
$this->assertSame($expectedData, $form->getData());
425425
}
426+
427+
/**
428+
* @dataProvider provideEmptyData
429+
*/
430+
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
431+
{
432+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
433+
'widget' => $widget,
434+
'empty_data' => $emptyData,
435+
));
436+
$form->submit(null);
437+
438+
$this->assertSame($emptyData, $form->getViewData());
439+
$this->assertEquals($expectedData, $form->getNormData());
440+
$this->assertEquals($expectedData, $form->getData());
441+
}
442+
443+
public function provideEmptyData()
444+
{
445+
$expectedData = \DateInterval::createFromDateString('6 years and 4 months');
446+
447+
return array(
448+
'Simple field' => array('single_text', 'P6Y4M0D', $expectedData),
449+
'Compound text field' => array('text', array('years' => '06', 'months' => '04', 'days' => '00'), $expectedData),
450+
'Compound integer field' => array('integer', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
451+
'Compound choice field' => array('choice', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
452+
);
453+
}
426454
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.