Skip to content

Commit dd89471

Browse files
committed
Add jquery-ui-timepicker-addon support
1 parent 40fa82f commit dd89471

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Form/Type/JqueryDatePickerType.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@ class JqueryDatePickerType extends AbstractType
2626
{
2727
public function buildForm(FormBuilderInterface $builder, array $options)
2828
{
29-
$format_php = $options['format'];
29+
$formatPhp = $options['format'];
30+
if ($options['time_format']) {
31+
if ($options['input'] !== 'datetime') {
32+
throw new InvalidConfigurationException('The time_format is compatible only with input = datetime');
33+
}
34+
35+
$formatPhp = \sprintf('%s %s', $formatPhp, $options['time_format']);
36+
}
3037

3138
$builder->addViewTransformer(
32-
new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $format_php)
39+
new DateTimeToStringTransformer($options['data_timezone'], $options['user_timezone'], $formatPhp)
3340
);
3441

3542
if ($options['input'] === 'string') {
@@ -79,6 +86,15 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7986
$view->vars['go_to_current'] = ($options['go_to_current']) ? 'true' : 'false';
8087
$view->vars['number_of_months'] = $options['number_of_months'];
8188
$view->vars['other'] = $options['other'];
89+
90+
$view->vars['time_format'] = null;
91+
$phpTimeFormat = $options['time_format'];
92+
if ($phpTimeFormat) {
93+
$phpTimes = array('a', 'A', 'g', 'G', 'h', 'H', 'i', 's', 'u', 'v');
94+
$jqueryTimes = array('t', 'T', 'h', 'H', 'hh', 'HH', 'mm','ss', 's', 'c', 'l');
95+
96+
$view->vars['time_format'] = str_replace($phpTimes, $jqueryTimes, $phpTimeFormat);
97+
}
8298
}
8399

84100

@@ -93,6 +109,7 @@ public function configureOptions(OptionsResolver $resolver)
93109
array(
94110
'input' => 'datetime',
95111
'format' => 'd/m/Y',
112+
'time_format' => null,
96113
'data_timezone' => null,
97114
'user_timezone' => null,
98115
'change_month' => true,

Resources/views/Form/fields.html.twig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
{% block ecommit_javascript_jquerydatepicker_widget %}
22
{% spaceless %}
33
{{ block('form_widget_simple') }}
4+
{% set jsFctName = (time_format) ? 'datetimepicker' : 'datepicker' %}
45
<script type="text/javascript">
56
$(function() {
6-
$("#{{ id }}").datepicker({
7+
$("#{{ id }}").{{ jsFctName }}({
78
dateFormat: '{{ date_format|raw }}',
9+
{% if time_format %}
10+
timeFormat: '{{ time_format }}',
11+
{% endif %}
812
changeMonth: {{ change_month }},
913
changeYear: {{ change_year }},
1014
firstDay: {{ first_day }},

0 commit comments

Comments
 (0)