8000 [Form] DateIntervalType: Allow to configure labels & enhance form theme · symfony/symfony@bfd9e50 · GitHub
[go: up one dir, main page]

Skip to content

Commit bfd9e50

Browse files
committed
[Form] DateIntervalType: Allow to configure labels & enhance form theme
1 parent e98c068 commit bfd9e50

File tree

4 files changed

+122
-16
lines changed

4 files changed

+122
-16
lines changed

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,32 @@
9494
{%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-inline')|trim}) -%}
9595
<div {{ block('widget_container_attributes') }}>
9696
{{- form_errors(form) -}}
97-
{%- if with_years %}{{ form_widget(form.years) }}{% endif -%}
98-
{%- if with_months %}{{ form_widget(form.months) }}{% endif -%}
99-
{%- if with_weeks %}{{ form_widget(form.weeks) }}{% endif -%}
100-
{%- if with_days %}{{ form_widget(form.days) }}{% endif -%}
101-
{%- if with_hours %}{{ form_widget(form.hours) }}{% endif -%}
102-
{%- if with_minutes %}{{ form_widget(form.minutes) }}{% endif -%}
103-
{%- if with_seconds %}{{ form_widget(form.seconds) }}{% endif -%}
97+
<div class="table-responsive">
98+
<table class="table {{ table_class|default('table-bordered table-condensed table-striped') }}">
99+
<thead>
100+
<tr>
101+
{%- if with_years %}<th>{{ form_label(form.years) }}</th>{% endif -%}
102+
{%- if with_months %}<th>{{ form_label(form.months) }}</th>{% endif -%}
103+
{%- if with_weeks %}<th>{{ form_label(form.weeks) }}</th>{% endif -%}
104+
{%- if with_days %}<th>{{ form_label(form.days) }}</th>{% endif -%}
105+
{%- if with_hours %}<th>{{ form_label(form.hours) }}</th>{% endif -%}
106+
{%- if with_minutes %}<th>{{ form_label(form.minutes) }}</th>{% endif -%}
107+
{%- if with_seconds %}<th>{{ form_label(form.seconds) }}</th>{% endif -%}
108+
</tr>
109+
</thead>
110+
<tbody>
111+
<tr>
112+
{%- if with_years %}<td>{{ form_widget(form.years) }}</td>{% endif -%}
113+
{%- if with_months %}<td>{{ form_widget(form.months) }}</td>{% endif -%}
114+
{%- if with_weeks %}<td>{{ form_widget(form.weeks) }}</td>{% endif -%}
115+
{%- if with_days %}<td>{{ form_widget(form.days) }}</td>{% endif -%}
116+
{%- if with_hours %}<td>{{ form_widget(form.hours) }}</td>{% endif -%}
117+
{%- if with_minutes %}<td>{{ form_widget(form.minutes) }}</td>{% endif -%}
118+
{%- if with_seconds %}<td>{{ form_widget(form.seconds) }}</td>{% endif -%}
119+
</tr>
120+
</tbody>
121+
</table>
122+
</div>
104123
{%- if with_invert %}{{ form_widget(form.invert) }}{% endif -%}
105124
</div>
106125
{%- endif -%}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,30 @@
136136
{%- else -%}
137137
<div {{ block('widget_container_attributes') }}>
138138
{{- form_errors(form) -}}
139-
{%- if with_years %}{{ form_widget(form.years) }}{% endif -%}
140-
{%- if with_months %}{{ form_widget(form.months) }}{% endif -%}
141-
{%- if with_weeks %}{{ form_widget(form.weeks) }}{% endif -%}
142-
{%- if with_days %}{{ form_widget(form.days) }}{% endif -%}
143-
{%- if with_hours %}{{ form_widget(form.hours) }}{% endif -%}
144-
{%- if with_minutes %}{{ form_widget(form.minutes) }}{% endif -%}
145-
{%- if with_seconds %}{{ form_widget(form.seconds) }}{% endif -%}
139+
<table class="{{ table_class|default('') }}">
140+
<thead>
141+
<tr>
142+
{%- if with_years %}<th>{{ form_label(form.years) }}</th>{% endif -%}
143+
{%- if with_months %}<th>{{ form_label(form.months) }}</th>{% endif -%}
144+
{%- if with_weeks %}<th>{{ form_label(form.weeks) }}</th>{% endif -%}
145+
{%- if with_days %}<th>{{ form_label(form.days) }}</th>{% endif -%}
146+
{%- if with_hours %}<th>{{ form_label(form.hours) }}</th>{% endif -%}
147+
{%- if with_minutes %}<th>{{ form_label(form.minutes) }}</th>{% endif -%}
148+
{%- if with_seconds %}<th>{{ form_label(form.seconds) }}</th>{% endif -%}
149+
</tr>
150+
</thead>
151+
<tbody>
152+
<tr>
153+
{%- if with_years %}<td>{{ form_widget(form.years) }}</td>{% endif -%}
154+
{%- if with_months %}<td>{{ form_widget(form.months) }}</td>{% endif -%}
155+
{%- if with_weeks %}<td>{{ form_widget(form.weeks) }}</td>{% endif -%}
156+
{%- if with_days %}<td>{{ form_widget(form.days) }}</td>{% endif -%}
157+
{%- if with_hours %}<td>{{ form_widget(form.hours) }}</td>{% endif -%}
158+
{%- if with_minutes %}<td>{{ form_widget(form.minutes) }}</td>{% endif -%}
159+
{%- if with_seconds %}<td>{{ form_widget(form.seconds) }}</td>{% endif -%}
160+
</tr>
161+
</tbody>
162+
</table>
146163
{%- if with_invert %}{{ form_widget(form.invert) }}{% endif -%}
147164
</div>
148165
{%- endif -%}

src/Symfony/Component/Form/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
/**

src/Symfony/Component/Form/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)
0