@@ -28,51 +28,67 @@ class TimeType extends AbstractType
28
28
*/
29
29
public function buildForm (FormBuilder $ builder , array $ options )
30
30
{
31
+ $ parts = array ('hour ' , 'minute ' );
32
+ $ format = 'H:i:00 ' ;
33
+ if ($ options ['with_seconds ' ]) {
34
+ $ format = 'H:i:s ' ;
35
+ $ parts [] = 'second ' ;
36
+ }
37
+
31
38
if ($ options ['widget ' ] === 'single_text ' ) {
32
- $ builder ->appendClientTransformer (new DateTimeToStringTransformer ($ options ['data_timezone ' ], $ options ['user_timezone ' ], 'H:i:s ' ));
33
- } else if ($ options ['widget ' ] === 'choice ' ) {
34
- if (is_array ($ options ['empty_value ' ])) {
35
- $ options ['empty_value ' ] = array_merge (array ('hour ' => null , 'minute ' => null , 'second ' => null ), $ options ['empty_value ' ]);
36
- } else {
37
- $ options ['empty_value ' ] = array ('hour ' => $ options ['empty_value ' ], 'minute ' => $ options ['empty_value ' ], 'second ' => $ options ['empty_value ' ]);
38
- }
39
+ $ builder ->appendClientTransformer (new DateTimeToStringTransformer ($ options ['data_timezone ' ], $ options ['user_timezone ' ], $ format ));
40
+ } else {
41
+ $ hourOptions = $ minuteOptions = $ secondOptions = array ();
39
42
40
- $ builder
41
- ->add ('hour ' , $ options ['widget ' ], array (
43
+ if ($ options ['widget ' ] === 'choice ' ) {
44
+ if (is_array ($ options ['empty_value ' ])) {
45
+ $ options ['empty_value ' ] = array_merge (array ('hour ' => null , 'minute ' => null , 'second ' => null ), $ options ['empty_value ' ]);
46
+ } else {
47
+ $ options ['empty_value ' ] = array ('hour ' => $ options ['empty_value ' ], 'minute ' => $ options ['empty_value ' ], 'second ' => $ options ['empty_value ' ]);
48
+ }
49
+
50
+ // Only pass a subset of the options to children
51
+ $ hourOptions = array (
42
52
'choice_list ' => new PaddedChoiceList (
43
53
array_combine ($ options ['hours ' ], $ options ['hours ' ]), 2 , '0 ' , STR_PAD_LEFT
44
54
),
45
55
'empty_value ' => $ options ['empty_value ' ]['hour ' ],
46
56
'required ' => $ options ['required ' ],
47
- ))
48
- -> add ( ' minute ' , $ options [ ' widget ' ], array (
57
+ );
58
+ $ minuteOptions = array (
49
59
'choice_list ' => new PaddedChoiceList (
50
60
array_combine ($ options ['minutes ' ], $ options ['minutes ' ]), 2 , '0 ' , STR_PAD_LEFT
51
61
),
52
62
'empty_value ' => $ options ['empty_value ' ]['minute ' ],
53
63
'required ' => $ options ['required ' ],
54
- ))
64
+ );
65
+
66
+ if ($ options ['with_seconds ' ]) {
67
+ $ secondOptions = array (
68
+ 'choice_list ' => new PaddedChoiceList (
69
+ array_combine ($ options ['seconds ' ], $ options ['seconds ' ]), 2 , '0 ' , STR_PAD_LEFT
70
+ ),
71
+ 'empty_value ' => $ options ['empty_value ' ]['second ' ],
72
+ 'required ' => $ options ['required ' ],
73
+ );
74
+ }
75
+ }
76
+
77
+ $ builder
78
+ ->add ('hour ' , $ options ['widget ' ], $ hourOptions )
79
+ ->add ('minute ' , $ options ['widget ' ], $ minuteOptions )
55
80
;
56
81
57
82
if ($ options ['with_seconds ' ]) {
58
- $ builder ->add ('second ' , $ options ['widget ' ], array (
59
- 'choice_list ' => new PaddedChoiceList (
60
- array_combine ($ options ['seconds ' ], $ options ['seconds ' ]), 2 , '0 ' , STR_PAD_LEFT
61
- ),
62
- 'empty_value ' => $ options ['empty_value ' ]['second ' ],
63
- 'required ' => $ options ['required ' ],
64
- ));
83
+ $ builder ->add ('second ' , $ options ['widget ' ], $ secondOptions );
65
84
}
66
- }
67
85
68
- $ parts = array ('hour ' , 'minute ' );
69
- if ($ options ['with_seconds ' ]) {
70
- $ parts [] = 'second ' ;
86
+ $ builder ->appendClientTransformer (new DateTimeToArrayTransformer ($ options ['data_timezone ' ], $ options ['user_timezone ' ], $ parts , $ options ['widget ' ] === 'text ' ));
71
87
}
72
88
73
89
if ($ options ['input ' ] === 'string ' ) {
74
90
$ builder ->appendNormTransformer (new ReversedTransformer (
75
- new DateTimeToStringTransformer ($ options ['data_timezone ' ], $ options ['data_timezone ' ], ' H:i:s ' )
91
+ new DateTimeToStringTransformer ($ options ['data_timezone ' ], $ options ['data_timezone ' ], $ format )
76
92
));
77
93
} else if ($ options ['input ' ] === 'timestamp ' ) {
78
94
$ builder ->appendNormTransformer (new ReversedTransformer (
@@ -84,15 +100,6 @@ public function buildForm(FormBuilder $builder, array $options)
84
100
));
85
101
}
86
102
87
- if ($ options ['widget ' ] !== 'single_text ' ) {
88
- $ builder ->appendClientTransformer (new DateTimeToArrayTransformer (
89
- $ options ['data_timezone ' ],
90
- $ options ['user_timezone ' ],
91
- $ parts ,
92
- $ options ['widget ' ] === 'text '
93
- ));
94
- }
95
-
96
103
$ builder
97
104
->setAttribute ('widget ' , $ options ['widget ' ])
98
105
->setAttribute ('with_seconds ' , $ options ['with_seconds ' ])
0 commit comments