Skip to content

Commit

Permalink
[Form] Fixed empty data for compound date interval
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Nov 18, 2018
1 parent 236565c commit 38a2abc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
40 changes: 18 additions & 22 deletions src/Symfony/Component/Form/Extension/Core/Type/DateIntervalType.php
Expand Up @@ -38,9 +38,9 @@ class DateIntervalType extends AbstractType
'seconds',
);
private static $widgets = array(
'text' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
'integer' => 'Symfony\Component\Form\Extension\Core\Type\IntegerType',
'choice' => 'Symfony\Component\Form\Extension\Core\Type\ChoiceType',
'text' => TextType::class,
'integer' => IntegerType::class,
'choice' => ChoiceType::class,
);

/**
Expand Down Expand Up @@ -96,31 +96,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
if ('single_text' === $options['widget']) {
$builder->addViewTransformer(new DateIntervalToStringTransformer($format));
} else {
$childOptions = array();
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childOptions[$part] = array(
$childOptions = array(
'error_bubbling' => true,
'label' => $options['labels'][$part],
// Append generic carry-along options
'required' => $options['required'],
'translation_domain' => $options['translation_domain'],
// when compound the array entries are ignored, we need to cascade the configuration here
'empty_data' => isset($options['empty_data'][$part]) ? $options['empty_data'][$part] : null,
);
if ('choice' === $options['widget']) {
$childOptions[$part]['choice_translation_domain'] = false;
$childOptions[$part]['choices'] = $options[$part];
$childOptions[$part]['placeholder'] = $options['placeholder'][$part];
$childOptions['choice_translation_domain'] = false;
$childOptions['choices'] = $options[$part];
$childOptions['placeholder'] = $options['placeholder'][$part];
}
}
}
// Append generic carry-along options
foreach (array('required', 'translation_domain') as $passOpt) {
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childOptions[$part][$passOpt] = $options[$passOpt];
}
}
}
foreach ($this->timeParts as $part) {
if ($options['with_'.$part]) {
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions[$part]);
$childForm = $builder->create($part, self::$widgets[$options['widget']], $childOptions);
if ('integer' === $options['widget']) {
$childForm->addModelTransformer(
new ReversedTransformer(
Expand All @@ -132,7 +124,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
}
if ($options['with_invert']) {
$builder->add('invert', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
$builder->add('invert', CheckboxType::class, array(
'label' => $options['labels']['invert'],
'error_bubbling' => true,
'required' => false,
Expand Down Expand Up @@ -180,6 +172,9 @@ public function configureOptions(OptionsResolver $resolver)
$compound = function (Options $options) {
return 'single_text' !== $options['widget'];
};
$emptyData = function (Options $options) {
return 'single_text' === $options['widget'] ? '' : array();
};

$placeholderDefault = function (Options $options) {
return $options['required'] ? null : '';
Expand Down Expand Up @@ -238,6 +233,7 @@ public function configureOptions(OptionsResolver $resolver)
// this option.
'data_class' => null,
'compound' => $compound,
'empty_data' => $emptyData,
'labels' => array(),
)
);
Expand Down
Expand Up @@ -423,4 +423,32 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = array(), $expect
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}

/**
* @dataProvider provideEmptyData
*/
public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedData)
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'widget' => $widget,
'empty_data' => $emptyData,
));
$form->submit(null);

$this->assertSame($emptyData, $form->getViewData());
$this->assertEquals($expectedData, $form->getNormData());
$this->assertEquals($expectedData, $form->getData());
}

public function provideEmptyData()
{
$expectedData = \DateInterval::createFromDateString('6 years and 4 months');

return array(
'Simple field' => array('single_text', 'P6Y4M0D', $expectedData),
'Compound text field' => array('text', array('years' => '06', 'months' => '04', 'days' => '00'), $expectedData),
'Compound integer field' => array('integer', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
'Compound choice field' => array('choice', array('years' => '6', 'months' => '4', 'days' => '0'), $expectedData),
);
}
}

0 comments on commit 38a2abc

Please sign in to comment.