File tree 2 files changed +18
-2
lines changed
2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change 8
8
class HashToKeyValueArrayTransformer implements DataTransformerInterface
9
9
{
10
10
11
+ private $ useContainerObject ;
12
+
13
+ /**
14
+ * @param bool $useContainerObject Whether to return a KeyValueContainer object or simply an array
15
+ */
16
+ public function __construct ($ useContainerObject )
17
+ {
18
+ $ this ->useContainerObject = $ useContainerObject ;
19
+ }
20
+
11
21
/**
12
22
* Doing the transformation here would be too late for the collection type to do it's resizing magic, so
13
23
* instead it is done in the forms PRE_SET_DATA listener
@@ -17,9 +27,14 @@ public function transform($value)
17
27
return $ value ;
18
28
}
19
29
30
+ /**
31
+ * @param mixed $value
32
+ * @return KeyValueContainer|array
33
+ * @throws \Symfony\Component\Form\Exception\TransformationFailedException
34
+ */
20
35
public function reverseTransform ($ value )
21
36
{
22
- $ return = new KeyValueContainer ();
37
+ $ return = $ this -> useContainerObject ? new KeyValueContainer () : array ();
23
38
24
39
foreach ($ value as $ data ) {
25
40
if (array ('key ' , 'value ' ) != array_keys ($ data )) {
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ class KeyValueType extends AbstractType
14
14
{
15
15
public function buildForm (FormBuilderInterface $ builder , array $ options )
16
16
{
17
- $ builder ->addModelTransformer (new HashToKeyValueArrayTransformer ());
17
+ $ builder ->addModelTransformer (new HashToKeyValueArrayTransformer ($ options [ ' use_container_object ' ] ));
18
18
19
19
$ builder ->addEventListener (FormEvents::PRE_SET_DATA , function (FormEvent $ e ) {
20
20
$ input = $ e ->getData ();
@@ -44,6 +44,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
44
44
'allow_delete ' => true ,
45
45
'value_options ' => array (),
46
46
'allowed_keys ' => null ,
47
+ 'use_container_object ' => false ,
47
48
'options ' => function (Options $ options ) {
48
49
return array (
49
50
'value_type ' => $ options ['value_type ' ],
You can’t perform that action at this time.
0 commit comments