@@ -51,13 +51,28 @@ public function buildForm(FormBuilderInterface $builder, array $options)
51
51
$ timeFormat = \IntlDateFormatter::NONE ;
52
52
$ calendar = \IntlDateFormatter::GREGORIAN ;
53
53
$ pattern = is_string ($ options ['format ' ]) ? $ options ['format ' ] : null ;
54
+ $ fields = array ('year ' , 'month ' , 'day ' );
54
55
55
56
if (!in_array ($ dateFormat , self ::$ acceptedFormats , true )) {
56
57
throw new InvalidOptionsException ('The "format" option must be one of the IntlDateFormatter constants (FULL, LONG, MEDIUM, SHORT) or a string representing a custom format. ' );
57
58
}
58
59
59
- if (null !== $ pattern && (false === strpos ($ pattern , 'y ' ) || false === strpos ($ pattern , 'M ' ) || false === strpos ($ pattern , 'd ' ))) {
60
- throw new InvalidOptionsException (sprintf ('The "format" option should contain the letters "y", "M" and "d". Its current value is "%s". ' , $ pattern ));
60
+ if (null !== $ pattern ) {
61
+ if (false === strpos ($ pattern , 'y ' )) {
62
+ unset($ fields [0 ]);
63
+ }
64
+
65
+ if (false === strpos ($ pattern , 'M ' )) {
66
+ unset($ fields [1 ]);
67
+ }
68
+
69
+ if (false === strpos ($ pattern , 'd ' )) {
70
+ unset($ fields [2 ]);
71
+ }
72
+
73
+ if (0 === count ($ fields )) {
74
+ throw new InvalidOptionsException (sprintf ('The "format" option should contain the letters "y", "M" or "d". Its current value is "%s". ' , $ pattern ));
75
+ }
61
76
}
62
77
63
78
if ('single_text ' === $ options ['widget ' ]) {
@@ -113,7 +128,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
113
128
->add ('month ' , self ::$ widgets [$ options ['widget ' ]], $ monthOptions )
114
129
->add ('day ' , self ::$ widgets [$ options ['widget ' ]], $ dayOptions )
115
130
->addViewTransformer (new DateTimeToArrayTransformer (
116
- $ options ['model_timezone ' ], $ options ['view_timezone ' ], array ( ' year ' , ' month ' , ' day ' )
131
+ $ options ['model_timezone ' ], $ options ['view_timezone ' ], $ fields
117
132
))
118
133
->setAttribute ('formatter ' , $ formatter )
119
134
;
@@ -129,7 +144,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
129
144
));
130
145
} elseif ('array ' === $ options ['input ' ]) {
131
146
$ builder ->addModelTransformer (new ReversedTransformer (
132
- new DateTimeToArrayTransformer ($ options ['model_timezone ' ], $ options ['model_timezone ' ], array ( ' year ' , ' month ' , ' day ' ) )
147
+ new DateTimeToArrayTransformer ($ options ['model_timezone ' ], $ options ['model_timezone ' ], $ fields )
133
148
));
134
149
}
135
150
}
0 commit comments