8000 [Form] Simplify DateIntervalType and add tests · symfony/symfony@0c44633 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c44633

Browse files
committed
[Form] Simplify DateIntervalType and add tests
1 parent 236565c commit 0c44633

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

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

Lines changed: 16 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,21 @@ 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'],
105107
);
106108
if ('choice' === $options['widget']) {
107-
$childOptions[$part]['choice_translation_domain'] = false;
108-
$childOptions[$part]['choices'] = $options[$part];
109-
$childOptions[$part]['placeholder'] = $options['placeholder'][$part];
109+
$childOptions['choice_translation_domain'] = false;
110+
$childOptions['choices'] = $options[$part];
111+
$childOptions['placeholder'] = $options['placeholder'][$part];
110112
}
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]);
113+
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions);
124114
if ('integer' === $options['widget']) {
125115
$childForm->addModelTransformer(
126116
new ReversedTransformer(
@@ -132,7 +122,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
132122
}
133123
}
134124
if ($options['with_invert']) {
135-
$builder->add('invert', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
125+
$builder->add('invert', CheckboxType::class, array(
136126
'label' => $options['labels']['invert'],
137127
'error_bubbling' => true,
138128
'required' => false,
@@ -180,6 +170,9 @@ public function configureOptions(OptionsResolver $resolver)
180170
$compound = function (Options $options) {
181171
return 'single_text' !== $options['widget'];
182172
};
173+
$emptyData = function (Options $options) {
174+
return 'single_text' === $options['widget'] ? '' : array();
175+
};
183176

184177
$placeholderDefault = function (Options $options) {
185178
return $options['required'] ? null : '';
@@ -238,6 +231,7 @@ public function configureOptions(OptionsResolver $resolver)
238231
// this option.
239232
'data_class' => null,
240233
'compound' => $compound,
234+
'empty_data' => $emptyData,
241235
'labels' => array(),
242236
)
243237
);

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,33 @@ 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 tests are broken for now
450+
//'Compound text field' => array('text', array('years' => '06', 'months' => '04', 'days' => '00'), $expectedData),
451+
//'Compound integer field' => array('integer', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
452+
//'Compound choice field' => array('choice', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
453+
);
454+
}
426455
}

0 commit comments

Comments
 (0)
0