Skip to content

Commit

Permalink
feature #21002 [Form] Added options for separate date/time labels in …
Browse files Browse the repository at this point in the history
…DateTimeType. (mktcode)

This PR was squashed before being merged into the 4.2-dev branch (closes #21002).

Discussion
----------

[Form] Added options for separate date/time labels in DateTimeType.

If your render date and time separately you need options for each label.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Let's say you have the following form field:

```
$builder
    ->add('start', DateTimeType::class, [
        'date_widget' => 'single_text',
        ...
    ])
    ...
```
Then you can render the date and time widgets/rows/etc. separately:

```
<div>{{ form_row(form.start.date) }}</div>
<div>{{ form_row(form.start.time) }}</div>
```
But you can't provide labels for each, so what is displayed is just the uppercased field name ("Date" and "Time").

This PR adds 'date_label' and 'time_label' options, so you can do:
```
$builder
    ->add('start', DateTimeType::class, [
        'date_widget' => 'single_text',
        'date_label' => 'The Start Date',
        'time_label' => 'The Start Time',
        ...
    ])
    ...
```

Commits
-------

df19155 [Form] Added options for separate date/time labels in DateTimeType.
  • Loading branch information
fabpot committed Aug 2, 2018
2 parents 6198223 + df19155 commit dd2f830
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Expand Up @@ -137,10 +137,18 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$dateOptions['widget'] = $options['date_widget'];
}

if (null !== $options['date_label']) {
$dateOptions['label'] = $options['date_label'];
}

if (null !== $options['time_widget']) {
$timeOptions['widget'] = $options['time_widget'];
}

if (null !== $options['time_label']) {
$timeOptions['label'] = $options['time_label'];
}

if (null !== $options['date_format']) {
$dateOptions['format'] = $options['date_format'];
}
Expand Down Expand Up @@ -235,6 +243,8 @@ public function configureOptions(OptionsResolver $resolver)
// this option.
'data_class' => null,
'compound' => $compound,
'date_label' => null,
'time_label' => null,
));

// Don't add some defaults in order to preserve the defaults
Expand Down

0 comments on commit dd2f830

Please sign in to comment.