55use Fp \JsFormValidatorBundle \Form \Constraint \UniqueEntity ;
66use Fp \JsFormValidatorBundle \Model \JsConfig ;
77use Fp \JsFormValidatorBundle \Model \JsFormElement ;
8+ use Symfony \Component \Form \ChoiceList \ChoiceListInterface ;
89use Symfony \Component \Form \DataTransformerInterface ;
9- use Symfony \Component \Form \Extension \Core \ChoiceList \ChoiceListInterface ;
10+ use Symfony \Component \Form \Extension \Core \DataTransformer \ChoicesToBooleanArrayTransformer ;
11+ use Symfony \Component \Form \Extension \Core \DataTransformer \ChoiceToBooleanArrayTransformer ;
1012use Symfony \Component \Form \Form ;
1113use Symfony \Component \Form \FormInterface ;
1214use Symfony \Component \Translation \TranslatorInterface ;
1315use Symfony \Component \Validator \Constraint ;
1416use Symfony \Component \Validator \Mapping \ClassMetadata ;
1517use Symfony \Component \Validator \Mapping \GetterMetadata ;
1618use Symfony \Component \Validator \Mapping \PropertyMetadata ;
17- use Symfony \Component \Validator \ValidatorInterface ;
19+ use Symfony \Component \Validator \Validator \ ValidatorInterface ;
1820
1921/**
2022 * This factory uses to parse a form to a tree of JsFormElement's
@@ -91,7 +93,7 @@ public function __construct(
9193 */
9294 protected function getMetadataFor ($ className )
9395 {
94- return $ this ->validator ->getMetadataFactory ()-> getMetadataFor ($ className );
96+ return $ this ->validator ->getMetadataFor ($ className );
9597 }
9698
9799 /**
@@ -247,7 +249,7 @@ public function createJsModel(Form $form)
247249 $ conf ->getOption ('invalid_message ' ),
248250 $ conf ->getOption ('invalid_message_parameters ' )
249251 );
250- $ model ->transformers = $ this ->parseTransformers ($ form -> getConfig ()-> getViewTransformers ());
252+ $ model ->transformers = $ this ->parseTransformers ($ this -> getViewTransformers ($ form ));
251253 $ model ->cascade = $ conf ->getOption ('cascade_validation ' );
252254 $ model ->bubbling = $ conf ->getOption ('error_bubbling ' );
253255 $ model ->data = $ this ->getValidationData ($ form );
@@ -315,8 +317,8 @@ protected function getValidationData(Form $form)
315317 $ parent = $ form ->getParent ();
316318 if ($ parent && null !== $ parent ->getConfig ()->getDataClass ()) {
317319 $ classMetadata = $ metadata = $ this ->getMetadataFor ($ parent ->getConfig ()->getDataClass ());
318- if ($ classMetadata ->hasMemberMetadatas ($ form ->getName ())) {
319- $ metadata = $ classMetadata ->getMemberMetadatas ($ form ->getName ());
320+ if ($ classMetadata ->hasPropertyMetadata ($ form ->getName ())) {
321+ $ metadata = $ classMetadata ->getPropertyMetadata ($ form ->getName ());
320322 /** @var PropertyMetadata $item */
321323 foreach ($ metadata as $ item ) {
322324 $ this ->composeValidationData (
@@ -448,6 +450,32 @@ protected function isProcessableElement($element)
448450 && ('hidden ' !== $ element ->getConfig ()->getType ()->getName ());
449451 }
450452
453+ /**
454+ * Gets view transformers from the given form.
455+ * Merges in an extra Choice(s)ToBooleanArrayTransformer transformer in case of expanded choice.
456+ *
457+ * @param FormInterface $form
458+ *
459+ * @return array
460+ */
461+ protected function getViewTransformers (FormInterface $ form )
462+ {
463+ $ config = $ form ->getConfig ();
464+ $ type = $ config ->getType ()->getInnerType ()->getName ();
465+ $ viewTransformers = $ config ->getViewTransformers ();
466+
467+ // Choice(s)ToBooleanArrayTransformer was deprecated in SF2.7 in favor of CheckboxListMapper and RadioListMapper
468+ if ($ type === 'choice ' && $ config ->getOption ('expanded ' )) {
469+ $ choiceList = $ config ->getOption ('choice_list ' );
470+ $ transformer = $ config ->getOption ('multiple ' )
471+ ? @new ChoicesToBooleanArrayTransformer ($ choiceList )
472+ : @new ChoiceToBooleanArrayTransformer ($ choiceList , $ config ->getOption ('placeholder ' ));
473+ array_unshift ($ viewTransformers , $ transformer );
474+ }
475+
476+ return $ viewTransformers ;
477+ }
478+
451479 /**
452480 * Convert transformers objects to data arrays
453481 *
@@ -471,7 +499,6 @@ protected function parseTransformers(array $transformers)
471499
472500 $ result [] = $ item ;
473501 }
474-
475502 return $ result ;
476503 }
477504
@@ -495,7 +522,7 @@ protected function getTransformerParam(DataTransformerInterface $transformer, $p
495522 } elseif (is_scalar ($ value ) || is_array ($ value )) {
496523 $ result = $ value ;
497524 } elseif ($ value instanceof ChoiceListInterface) {
498- $ result = $ value ->getChoices ();
525+ $ result = array_values ( $ value ->getChoices () );
499526 }
500527
501528 return $ result ;
0 commit comments