8
8
use Symfony \Component \Form \FormEvent ;
9
9
use Symfony \Component \Form \FormEvents ;
10
10
use Symfony \Component \OptionsResolver \Options ;
11
+ use Symfony \Component \OptionsResolver \OptionsResolver ;
11
12
use Symfony \Component \OptionsResolver \OptionsResolverInterface ;
12
13
13
14
class KeyValueType extends AbstractType
@@ -38,16 +39,24 @@ public function buildForm(FormBuilderInterface $builder, array $options)
38
39
39
40
public function setDefaultOptions (OptionsResolverInterface $ resolver )
40
41
{
42
+ $ this ->configureOptions ($ resolver );
43
+ }
44
+
45
+ public function configureOptions (OptionsResolver $ resolver )
46
+ {
47
+ // check if Form component version 2.8+ is used
48
+ $ isSf28 = method_exists ('Symfony\Component\Form\AbstractType ' , 'getBlockPrefix ' );
49
+
41
50
$ resolver ->setDefaults (array (
42
- 'type ' => 'burgov_key_value_row ' ,
51
+ $ isSf28 ? ' entry_type ' : 'type ' => 'burgov_key_value_row ' ,
43
52
'allow_add ' => true ,
44
53
'allow_delete ' => true ,
45
54
'key_type ' => 'text ' ,
46
55
'key_options ' => array (),
47
56
'value_options ' => array (),
48
57
'allowed_keys ' => null ,
49
58
'use_container_object ' => false ,
50
- 'options ' => function (Options $ options ) {
59
+ $ isSf28 ? ' entry_options ' : 'options ' => function (Options $ options ) {
51
60
return array (
52
61
'key_type ' => $ options ['key_type ' ],
53
62
'value_type ' => $ options ['value_type ' ],
@@ -59,15 +68,27 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
59
68
));
60
69
61
70
$ resolver ->setRequired (array ('value_type ' ));
62
- $ resolver ->setAllowedTypes (array ('allowed_keys ' => array ('null ' , 'array ' )));
71
+
72
+ if (method_exists ($ resolver , 'setDefined ' )) {
73
+ // Symfony 2.6+ API
74
+ $ resolver ->setAllowedTypes ('allowed_keys ' => array ('null ' , 'array ' ));
75
+ } else {
76
+ // Symfony <2.6 API
77
+ $ resolver ->setAllowedTypes (array ('allowed_keys ' => array ('null ' , 'array ' )));
78
+ }
63
79
}
64
80
65
81
public function getParent ()
66
82
{
67
- return 'collection ' ;
83
+ return method_exists ( ' Symfony\Component\Form\AbstractType ' , ' getBlockPrefix ' ) ? ' Symfony\Component\Form\Extension\Core\Type\CollectionType ' : 'collection ' ;
68
84
}
69
85
70
86
public function getName ()
87
+ {
88
+ return $ this ->getBlockPrefix ();
89
+ }
90
+
91
+ public function getBlockPrefix ()
71
92
{
72
93
return 'burgov_key_value ' ;
73
94
}
0 commit comments