Skip to content

Commit 08fa9f6

Browse files
committed
Merge pull request #15 from WouterJ/sf-28
Add Symfony 3 support
2 parents f522503 + 25bd195 commit 08fa9f6

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

Form/Type/KeyValueRowType.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Burgov\Bundle\KeyValueFormBundle\Form\Type;
44

5-
65
use Symfony\Component\Form\AbstractType;
76
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
87
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
99
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1010

1111
class KeyValueRowType extends AbstractType
@@ -25,11 +25,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2525
}
2626

2727
public function getName()
28+
{
29+
return $this->getBlockPrefix();
30+
}
31+
32+
public function getBlockPrefix()
2833
{
2934
return 'burgov_key_value_row';
3035
}
3136

3237
public function setDefaultOptions(OptionsResolverInterface $resolver)
38+
{
39+
$this->configureOptions($resolver);
40+
}
41+
42+
public function configureOptions(OptionsResolver $resolver)
3343
{
3444
$resolver->setDefaults(array(
3545
'key_type' => 'text',
@@ -39,6 +49,13 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
3949
));
4050

4151
$resolver->setRequired(array('value_type'));
42-
$resolver->setAllowedTypes(array('allowed_keys' => array('null', 'array')));
52+
53+
if (method_exists($resolver, 'setDefined')) {
54+
// Symfony 2.6+ API
55+
$resolver->setAllowedTypes('allowed_keys', array('null', 'array'));
56+
} else {
57+
// Symfony <2.6 API
58+
$resolver->setAllowedTypes(array('allowed_keys' => array('null', 'array')));
59+
}
4360
}
4461
}

Form/Type/KeyValueType.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Form\FormEvent;
99
use Symfony\Component\Form\FormEvents;
1010
use Symfony\Component\OptionsResolver\Options;
11+
use Symfony\Component\OptionsResolver\OptionsResolver;
1112
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
1213

1314
class KeyValueType extends AbstractType
@@ -38,16 +39,24 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3839

3940
public function setDefaultOptions(OptionsResolverInterface $resolver)
4041
{
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+
4150
$resolver->setDefaults(array(
42-
'type' => 'burgov_key_value_row',
51+
$isSf28 ? 'entry_type' : 'type' => 'burgov_key_value_row',
4352
'allow_add' => true,
4453
'allow_delete' => true,
4554
'key_type' => 'text',
4655
'key_options' => array(),
4756
'value_options' => array(),
4857
'allowed_keys' => null,
4958
'use_container_object' => false,
50-
'options' => function(Options $options) {
59+
$isSf28 ? 'entry_options' : 'options' => function(Options $options) {
5160
return array(
5261
'key_type' => $options['key_type'],
5362
'value_type' => $options['value_type'],
@@ -59,15 +68,27 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
5968
));
6069

6170
$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+
}
6379
}
6480

6581
public function getParent()
6682
{
67-
return 'collection';
83+
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\CollectionType' : 'collection';
6884
}
6985

7086
public function getName()
87+
{
88+
return $this->getBlockPrefix();
89+
}
90+
91+
public function getBlockPrefix()
7192
{
7293
return 'burgov_key_value';
7394
}

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"require": {
1010
"php": ">=5.3.3",
11-
"symfony/form": "~2.3"
11+
"symfony/form": "~2.3|3.*"
1212
},
1313
"extra": {
1414
"branch-alias": {

0 commit comments

Comments
 (0)