Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90a0ab0

Browse files
committed
[Form] Fixed empty data for compound date types
1 parent 131a42b commit 90a0ab0

File tree

6 files changed

+122
-9
lines changed

6 files changed

+122
-9
lines changed

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9090
));
9191
}
9292
} else {
93+
// when the form is compound the entries of the array as ignored in favor of children data
94+
// so we need to handle the cascade setting here
95+
$emptyData = $builder->getEmptyData() ?: array();
9396
// Only pass a subset of the options to children
9497
$dateOptions = array_intersect_key($options, array_flip(array(
9598
'years',
@@ -105,6 +108,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
105108
'invalid_message_parameters',
106109
)));
107110

111+
if (isset($emptyData['date'])) {
112+
$dateOptions['empty_data'] = $emptyData['date'];
113+
}
114+
108115
$timeOptions = array_intersect_key($options, array_flip(array(
109116
'hours',
110117
'minutes',
@@ -121,6 +128,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
121128
'invalid_message_parameters',
122129
)));
123130

131+
if (isset($emptyData['time'])) {
132+
$timeOptions['empty_data'] = $emptyData['time'];
133+
}
134+
124135
if (false === $options['label']) {
125136
$dateOptions['label'] = false;
126137
$timeOptions['label'] = false;
@@ -226,6 +237,9 @@ public function configureOptions(OptionsResolver $resolver)
226237
// this option.
227238
'data_class' => null,
228239
'compound' => $compound,
240+
'empty_data' => function (Options $options) {
241+
return $options['compound'] ? array() : '';
242+
},
229243
));
230244

231245
// Don't add some defaults in order to preserve the defaults

src/Symfony/Component/Form/Extension/Core/Type/DateType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7575

7676
$yearOptions = $monthOptions = $dayOptions = array(
7777
'error_bubbling' => true,
78+
'empty_data' => '',
7879
);
80+
// when the form is compound the entries of the array as ignored in favor of children data
81+
// so we need to handle the cascade setting here
82+
$emptyData = $builder->getEmptyData() ?: array();
83+
84+
if (isset($emptyData['year'])) {
85+
$yearOptions['empty_data'] = $emptyData['year'];
86+
}
87+
if (isset($emptyData['month'])) {
88+
$monthOptions['empty_data'] = $emptyData['month'];
89+
}
90+
if (isset($emptyData['day'])) {
91+
$dayOptions['empty_data'] = $emptyData['day'];
92+
}
7993

8094
if (isset($options['invalid_message'])) {
8195
$dayOptions['invalid_message'] = $options['invalid_message'];
@@ -272,6 +286,9 @@ public function configureOptions(OptionsResolver $resolver)
272286
// this option.
273287
'data_class' => null,
274288
'compound' => $compound,
289+
'empty_data' => function (Options $options) {
290+
return $options['compound'] ? array() : '';
291+
},
275292
'choice_translation_domain' => false,
276293
));
277294

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7070
} else {
7171
$hourOptions = $minuteOptions = $secondOptions = array(
7272
'error_bubbling' => true,
73+
'empty_data' => '',
7374
);
75+
// when the form is compound the entries of the array as ignored in favor of children data
76+
// so we need to handle the cascade setting here
77+
$emptyData = $builder->getEmptyData() ?: array();
78+
79+
if (isset($emptyData['hour'])) {
80+
$hourOptions['empty_data'] = $emptyData['hour'];
81+
}
7482

7583
if (isset($options['invalid_message'])) {
7684
$hourOptions['invalid_message'] = $options['invalid_message'];
@@ -138,10 +146,16 @@ public function buildForm(FormBuilderInterface $builder, array $options)
138146
$builder->add('hour', self::$widgets[$options['widget']], $hourOptions);
139147

140148
if ($options['with_minutes']) {
149+
if (isset($emptyData['minute'])) {
150+
$minuteOptions['empty_data'] = $emptyData['minute'];
151+
}
141152
$builder->add('minute', self::$widgets[$options['widget']], $minuteOptions);
142153
}
143154

144155
if ($options['with_seconds']) {
156+
if (isset($emptyData['second'])) {
157+
$secondOptions['empty_data'] = $emptyData['second'];
158+
}
145159
$builder->add('second', self::$widgets[$options['widget']], $secondOptions);
146160
}
147161

@@ -265,6 +279,9 @@ public function configureOptions(OptionsResolver $resolver)
265279
// representation is not \DateTime, but an array, we need to unset
266280
// this option.
267281
'data_class' => null,
282+
'empty_data' => function (Options $options) {
283+
return $options['compound'] ? array() : '';
284+
},
268285
'compound' => $compound,
269286
'choice_translation_domain' => false,
270287
));

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,4 +631,31 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
631631
$this->assertSame($expectedData, $form->getNormData());
632632
$this->assertSame($expectedData, $form->getData());
633633
}
634+
635+
/**
636+
* @dataProvider provideEmptyData
637+
*/
638+
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
639+
{
640+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
641+
'widget' => $widget,
642+
'empty_data' => $emptyData,
643+
));
644+
$form->submit(null);
645+
646+
$this->assertSame($emptyData, $form->getViewData());
647+
$this->assertEquals($expectedData, $form->getNormData());
648+
$this->assertEquals($expectedData, $form->getData());
649+
}
650+
651+
public function provideEmptyData()
652+
{
653+
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '2018-11-11 21:23');
654+
655+
return [
656+
'Simple field' => array('single_text', '2018-11-11T21:23:00', $expectedData),
657+
'Compound text field' => array('text', array('date' => array('year' => '2018', 'month' => '11', 'day' => '11'), 'time' => array('hour' => '21', 'minute' => '23')), $expectedData),
658+
'Compound choice field' => array('choice', array('date' => array('year' => '2018', 'month' => '11', 'day' => '11'), 'time' => array('hour' => '21', 'minute' => '23')), $expectedData),
659+
];
660+
}
634661
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,25 +1011,36 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
10111011
));
10121012
$form->submit(null);
10131013

1014-
// view transformer write back empty strings in the view data
1014+
// view transformer writes back empty strings in the view data
10151015
$this->assertSame(array('year' => '', 'month' => '', 'day' => ''), $form->getViewData());
10161016
$this->assertSame($expectedData, $form->getNormData());
10171017
$this->assertSame($expectedData, $form->getData());
10181018
}
10191019

1020-
public function testSingleTextSubmitNullUsesDefaultEmptyData()
1020+
/**
1021+
* @dataProvider provideEmptyData
1022+
*/
1023+
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
10211024
{
1022-
$emptyData = '2018-11-11';
10231025
$form = $this->factory->create(static::TESTED_TYPE, null, array(
1024-
'widget' => 'single_text',
1026+
'widget' => $widget,
10251027
'empty_data' => $emptyData,
10261028
));
10271029
$form->submit(null);
10281030

1029-
$date = new \DateTime($emptyData);
1030-
10311031
$this->assertSame($emptyData, $form->getViewData());
1032-
$this->assertEquals($date, $form->getNormData());
1033-
$this->assertEquals($date, $form->getData());
1032+
$this->assertEquals($expectedData, $form->getNormData());
1033+
$this->assertEquals($expectedData, $form->getData());
1034+
}
1035+
1036+
public function provideEmptyData()
1037+
{
1038+
$expectedData = \DateTime::createFromFormat('Y-m-d H:i:s', '2018-11-11 00:00:00');
1039+
1040+
return [
1041+
'Simple field' => array('single_text', '2018-11-11', $expectedData),
1042+
'Compound text fields' => array('text', array('year' => '2018', 'month' => '11', 'day' => '11'), $expectedData),
1043+
'Compound choice fields' => array('choice', array('year' => '2018', 'month' => '11', 'day' => '11'), $expectedData),
1044+
];
10341045
}
10351046
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,36 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
805805
));
806806
$form->submit(null);
807807

808-
// view transformer write back empty strings in the view data
808+
// view transformer writes back empty strings in the view data
809809
$this->assertSame(array('hour' => '', 'minute' => ''), $form->getViewData());
810810
$this->assertSame($expectedData, $form->getNormData());
811811
$this->assertSame($expectedData, $form->getData());
812812
}
813+
814+
/**
815+
* @dataProvider provideEmptyData
816+
*/
817+
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
818+
{
819+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
820+
'widget' => $widget,
821+
'empty_data' => $emptyData,
822+
));
823+
$form->submit(null);
824+
825+
$this->assertSame($emptyData, $form->getViewData());
826+
$this->assertEquals($expectedData, $form->getNormData());
827+
$this->assertEquals($expectedData, $form->getData());
828+
}
829+
830+
public function provideEmptyData()
831+
{
832+
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '1970-01-01 21:23');
833+
834+
return [
835+
'Simple field' => array('single_text', '21:23', $expectedData),
836+
'Compound text field' => array('text', array('hour' => '21', 'minute' => '23'), $expectedData),
837+
'Compound choice field' => array('choice', array('hour' => '21', 'minute' => '23'), $expectedData),
838+
];
839+
}
813840
}

0 commit comments

Comments
 (0)