Skip to content

Commit db657b7

Browse files
committed
feature #20887 [Form] DateIntervalType: Allow to configure labels & enhance form theme (ogizanagi)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Form] DateIntervalType: Allow to configure labels & enhance form theme | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no (unless someone relies on this non themed type) | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | Should document the new `labels` option I just realized by using it for last fixes in #20886 and #20877 that this type was not really themed: ### before <img width="861" alt="screenshot 2016-12-13 a 00 54 35" src="https://cloud.githubusercontent.com/assets/2211145/21121792/c589d27a-c0ce-11e6-8368-a396fda3bc7a.PNG"> At least labels should appear, but this also means being able to change them (thus the new `labels` option). I think the form themes should provide a functional & minimalistic integration like this: ### after <img width="862" alt="screenshot 2016-12-13 a 00 54 17" src="https://cloud.githubusercontent.com/assets/2211145/21121814/d9c4ead6-c0ce-11e6-94e1-41e6c14884a7.PNG"> --- (On screenshots above, I've only added a css rule to remove the 100% width of the `.table` class. See symfony/symfony#20887 (comment)) Commits ------- bfd9e50bbb [Form] DateIntervalType: Allow to configure labels & enhance form theme
2 parents 1ef4fc9 + 41b2ac8 commit db657b7

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

Extension/Core/Type/DateIntervalType.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9999
$childOptions = array();
100100
foreach ($this->timeParts as $part) {
101101
if ($options['with_'.$part]) {
102-
$childOptions[$part] = array();
103-
$childOptions[$part]['error_bubbling'] = true;
102+
$childOptions[$part] = array(
103+
'error_bubbling' => true,
104+
'label' => $options['labels'][$part],
105+
);
104106
if ('choice' === $options['widget']) {
105107
$childOptions[$part]['choice_translation_domain'] = false;
106108
$childOptions[$part]['choices'] = $options[$part];
@@ -131,6 +133,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
131133
}
132134
if ($options['with_invert']) {
133135
$builder->add('invert', 'Symfony\Component\Form\Extension\Core\Type\CheckboxType', array(
136+
'label' => $options['labels']['invert'],
134137
'error_bubbling' => true,
135138
'required' => false,
136139
'translation_domain' => $options['translation_domain'],
@@ -192,6 +195,21 @@ public function configureOptions(OptionsResolver $resolver)
192195
return array_fill_keys($timeParts, $placeholder);
193196
};
194197

198+
$labelsNormalizer = function (Options $options, array $labels) {
199+
return array_replace(array(
200+
'years' => null,
201+
'months' => null,
202+
'days' => null,
203+
'weeks' => null,
204+
'hours' => null,
205+
'minutes' => null,
206+
'seconds' => null,
207+
'invert' => 'Negative interval',
208+
), array_filter($labels, function ($label) {
209+
return null !== $label;
210+
}));
211+
};
212+
195213
$resolver->setDefaults(
196214
array(
197215
'with_years' => true,
@@ -220,9 +238,11 @@ public function configureOptions(OptionsResolver $resolver)
220238
// this option.
221239
'data_class' => null,
222240
'compound' => $compound,
241+
'labels' => array(),
223242
)
224243
);
225244
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
245+
$resolver->setNormalizer('labels', $labelsNormalizer);
226246

227247
$resolver->setAllowedValues(
228248
'input',
@@ -260,6 +280,7 @@ public function configureOptions(OptionsResolver $resolver)
260280
$resolver->setAllowedTypes('with_minutes', 'bool');
261281
$resolver->setAllowedTypes('with_seconds', 'bool');
262282
$resolver->setAllowedTypes('with_invert', 'bool');
283+
$resolver->setAllowedTypes('labels', 'array');
263284
}
264285

265286
/**

Tests/Extension/Core/Type/DateIntervalTypeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,53 @@ public function testInvertDoesNotInheritRequiredOption()
399399
);
400400
$this->assertFalse($form->get('invert')->getConfig()->getOption('required'));
401401
}
402+
403+
public function testCanChangeTimeFieldsLabels()
404+
{
405+
$form = $this->factory->create(
406+
DateIntervalType::class,
407+
null,
408+
array(
409+
'required' => true,
410+
'with_invert' => true,
411+
'with_hours' => true,
412+
'with_minutes' => true,
413+
'with_seconds' => true,
414+
'labels' => array(
415+
'invert' => 'form.trans.invert',
416+
'years' => 'form.trans.years',
417+
'months' => 'form.trans.months',
418+
'days' => 'form.trans.days',
419+
'hours' => 'form.trans.hours',
420+
'minutes' => 'form.trans.minutes',
421+
'seconds' => 'form.trans.seconds',
422+
),
423+
)
424+
);
425+
426+
$view = $form->createView();
427+
$this->assertSame('form.trans.invert', $view['invert']->vars['label']);
428+
$this->assertSame('form.trans.years', $view['years']->vars['label']);
429+
$this->assertSame('form.trans.months', $view['months']->vars['label']);
430+
$this->assertSame('form.trans.days', $view['days']->vars['label']);
431+
$this->assertSame('form.trans.hours', $view['hours']->vars['label']);
432+
$this->assertSame('form.trans.minutes', $view['minutes']->vars['label']);
433+
$this->assertSame('form.trans.seconds', $view['seconds']->vars['label']);
434+
}
435+
436+
public function testInvertDefaultLabel()
437+
{
438+
$form = $this->factory->create(DateIntervalType::class, null, array('with_invert' => true));
439+
440+
$view = $form->createView();
441+
$this->assertSame('Negative interval', $view['invert']->vars['label']);
442+
443+
$form = $this->factory->create(DateIntervalType::class, null, array(
444+
'with_invert' => true,
445+
'labels' => array('invert' => null),
446+
));
447+
448+
$view = $form->createView();
449+
$this->assertSame('Negative interval', $view['invert']->vars['label']);
450+
}
402451
}

0 commit comments

Comments
 (0)