1414use Symfony \Component \Form \Extension \Core \Type \ChoiceType ;
1515use Symfony \Component \Form \Extension \Core \Type \HiddenType ;
1616use Symfony \Component \Form \FormBuilderInterface ;
17+ use Symfony \Component \Form \FormEvent ;
18+ use Symfony \Component \Form \FormEvents ;
1719use Symfony \Component \Form \FormInterface ;
1820use Symfony \Component \OptionsResolver \OptionsResolver ;
1921use Symfony \Component \Validator \Constraint ;
@@ -51,7 +53,7 @@ public function configureOptions(OptionsResolver $resolver)
5153 {
5254 $ resolver ->setRequired ([
5355 self ::OPTION_ATTRIBUTE_TYPE_CHOICES ,
54- self ::OPTION_VALUES_CHOICES ,
56+ static ::OPTION_VALUES_CHOICES ,
5557 ]);
5658
5759 $ resolver ->setDefaults ([
@@ -66,7 +68,7 @@ public function configureOptions(OptionsResolver $resolver)
6668 !isset ($ submittedData [self ::FIELD_ALLOW_INPUT ]) || !$ submittedData [self ::FIELD_ALLOW_INPUT ] ||
6769 (isset ($ submittedData [self ::FIELD_IS_SUPER ]) && $ submittedData [self ::FIELD_IS_SUPER ])
6870 ) {
69- $ groups [] = self ::GROUP_VALUES ;
71+ $ groups [] = static ::GROUP_VALUES ;
7072 }
7173
7274 if (!isset ($ originalData [self ::FIELD_KEY ]) || $ submittedData [self ::FIELD_KEY ] !== $ originalData [self ::FIELD_KEY ]) {
@@ -188,22 +190,56 @@ protected function addIsSuperField(FormBuilderInterface $builder, array $options
188190 */
189191 protected function addValuesField (FormBuilderInterface $ builder , array $ options )
190192 {
191- $ builder ->add (self ::FIELD_VALUES , Select2ComboBoxType::class, [
193+ $ builder ->add (static ::FIELD_VALUES , Select2ComboBoxType::class, [
192194 'label ' => 'Predefined Values ' ,
193- 'choices ' => $ options [self ::OPTION_VALUES_CHOICES ],
195+ 'choices ' => $ options [static ::OPTION_VALUES_CHOICES ],
194196 'multiple ' => true ,
195197 'constraints ' => [
196198 new NotBlank ([
197- 'groups ' => self ::GROUP_VALUES ,
199+ 'groups ' => static ::GROUP_VALUES ,
198200 ]),
199201 ],
200202 ]);
201203
202- $ builder ->get (self ::FIELD_VALUES )->resetViewTransformers ();
204+ $ builder ->addEventListener (
205+ FormEvents::PRE_SUBMIT ,
206+ function (FormEvent $ event ) use ($ options ) {
207+ $ this ->addValuesFieldChoices ($ event , $ options );
208+ }
209+ );
203210
204211 return $ this ;
205212 }
206213
214+ /**
215+ * @param \Symfony\Component\Form\FormEvent $event
216+ * @param array $options
217+ *
218+ * @return void
219+ */
220+ protected function addValuesFieldChoices (FormEvent $ event , array $ options ): void
221+ {
222+ $ form = $ event ->getForm ();
223+ $ data = $ event ->getData ();
224+ $ data = $ data [static ::FIELD_VALUES ] ?? [];
225+ $ choices = $ options [static ::OPTION_VALUES_CHOICES ];
226+
227+ foreach ($ data as $ choice ) {
228+ $ choices [$ choice ] = $ choice ;
229+ }
230+
231+ $ form ->add (static ::FIELD_VALUES , Select2ComboBoxType::class, [
232+ 'label ' => 'Predefined Values ' ,
233+ 'choices ' => $ choices ,
234+ 'multiple ' => true ,
235+ 'constraints ' => [
236+ new NotBlank ([
237+ 'groups ' => static ::GROUP_VALUES ,
238+ ]),
239+ ],
240+ ]);
241+ }
242+
207243 /**
208244 * @param \Symfony\Component\Form\FormBuilderInterface $builder
209245 * @param array $options
0 commit comments