8000 [Form] Fixed empty data for compound date interval by HeahDude · Pull Request #29249 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed empty data for compound date 8000 interval #29249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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