Skip to content

Commit f522503

Browse files
committed
Merge pull request #14 from hason/keytype
Added key_type option
2 parents 785962c + dfc2a9c commit f522503

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

Form/Type/KeyValueRowType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ class KeyValueRowType extends AbstractType
1313
public function buildForm(FormBuilderInterface $builder, array $options)
1414
{
1515
if (null === $options['allowed_keys']) {
16-
$builder->add('key', 'text', $options['key_options']
17-
);
16+
$builder->add('key', $options['key_type'], $options['key_options']);
1817
} else {
1918
$builder->add('key', 'choice', array_merge(array(
2019
'choice_list' => new SimpleChoiceList($options['allowed_keys'])
@@ -33,6 +32,7 @@ public function getName()
3332
public function setDefaultOptions(OptionsResolverInterface $resolver)
3433
{
3534
$resolver->setDefaults(array(
35+
'key_type' => 'text',
3636
'key_options' => array(),
3737
'value_options' => array(),
3838
'allowed_keys' => null
@@ -41,4 +41,4 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
4141
$resolver->setRequired(array('value_type'));
4242
$resolver->setAllowedTypes(array('allowed_keys' => array('null', 'array')));
4343
}
44-
}
44+
}

Form/Type/KeyValueType.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
4242
'type' => 'burgov_key_value_row',
4343
'allow_add' => true,
4444
'allow_delete' => true,
45+
'key_type' => 'text',
4546
'key_options' => array(),
4647
'value_options' => array(),
4748
'allowed_keys' => null,
4849
'use_container_object' => false,
4950
'options' => function(Options $options) {
5051
return array(
52+
'key_type' => $options['key_type'],
5153
'value_type' => $options['value_type'],
5254
'key_options' => $options['key_options'],
5355
'value_options' => $options['value_options'],
@@ -69,4 +71,4 @@ public function getName()
6971
{
7072
return 'burgov_key_value';
7173
}
72-
}
74+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ The type adds four options to the collection type options, of which one is requi
4747

4848
* `value_type` (required) defines which form type to use to render the value field
4949
* `value_options` optional options to the child defined in `value_type`
50+
* `key_type` defines which form type to use to render the key field (default is a `text` field)
51+
* `key_options` optional options to the child defined in `key_type`
5052
* `allowed_keys` if this option is provided, the key field (which is usually a simple text field) will change to a `choice` field, and allow only those values you supplied in the this option.
5153
* `use_container_object` see explanation below at 'The KeyValueCollection'
5254

Tests/Form/Type/KeyValueTypeTest.php

+46-9
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ public function testSubmitValidData()
5050

5151
$form = $builder->getForm();
5252

53-
$this->assertFormTypes(array('text', 'text'), $form);
53+
$this->assertFormTypes(array('text', 'text'), array('text', 'text'), $form);
5454
$this->assertFormOptions(array(array('label' => 'label_key'), array('label' => 'label_value')), $form);
5555

5656
$form->submit($submitData);
5757
$this->assertTrue($form->isValid(), $form->getErrorsAsString());
5858

59-
$this->assertFormTypes(array('text', 'text', 'text'), $form);
59+
$this->assertFormTypes(array('text', 'text', 'text'), array('text', 'text', 'text'), $form);
6060
$this->assertFormOptions(array(array('label' => 'label_key'), array('label' => 'label_value')), $form);
6161

6262
$this->assertSame($expectedData, $form->getData());
@@ -82,7 +82,7 @@ public function testWithChoiceType()
8282

8383
$form = $builder->getForm();
8484

85-
$this->assertFormTypes(array(), $form);
85+
$this->assertFormTypes(array(), array(), $form);
8686
$this->assertFormOptions(array(array('label' => 'label_key'), array('label' => 'label_value')), $form);
8787

8888
$form->submit(array(
@@ -96,19 +96,56 @@ public function testWithChoiceType()
9696
)
9797
));
9898

99-
$this->assertFormTypes(array('choice', 'choice'), $form);
99+
$this->assertFormTypes(array('text', 'text'), array('choice', 'choice'), $form);
100100
$this->assertFormOptions(array(array('label' => 'label_key'), array('label' => 'label_value')), $form);
101101

102102
$this->assertTrue($form->isValid());
103103

104104
$this->assertSame(array('key1' => $obj2, 'key2' => $obj1), $form->getData());
105105
}
106106

107-
private function assertFormTypes(array $types, $form)
107+
public function testWithCustomKeyType()
108108
{
109-
$this->assertCount(count($types), $form);
110-
foreach ($types as $key => $type) {
111-
$this->assertEquals($type, $form->get($key)->get('value')->getConfig()->getType()->getInnerType()->getName());
109+
$builder = $this->factory->createBuilder('burgov_key_value', null, array(
110+
'key_type' => 'country',
111+
'value_type' => 'integer',
112+
'key_options' => array('label' => 'label_key'),
113+
));
114+
115+
$form = $builder->getForm();
116+
117+
$this->assertFormTypes(array(), array(), $form);
118+
$this->assertFormOptions(array(array('label' => 'label_key'), array()), $form);
119+
120+
$form->submit(array(
121+
array(
122+
'key' => 'GB',
123+
'value' => '2'
124+
),
125+
array(
126+
'key' => 'CZ',
127+
'value' => '1'
128+
)
129+
));
130+
131+
$this->assertFormTypes(array('country', 'country'), array('integer', 'integer'), $form);
132+
$this->assertFormOptions(array(array('label' => 'label_key'), array()), $form);
133+
134+
$this->assertTrue($form->isValid());
135+
136+
$this->assertSame(array('GB' => 2, 'CZ' => 1), $form->getData());
137+
}
138+
139+
private function assertFormTypes(array $keys, array $values, $form)
140+
{
141+
$this->assertCount(count($values), $form);
142+
for ($i = 0; $i < count($form); $i++) {
143+
if (isset($keys[$i])) {
144+
$this->assertEquals($keys[$i], $form->get($i)->get('key')->getConfig()->getType()->getInnerType()->getName());
145+
}
146+
if (isset($values[$i])) {
147+
$this->assertEquals($values[$i], $form->get($i)->get('value')->getConfig()->getType()->getInnerType()->getName());
148+
}
112149
}
113150
}
114151

@@ -135,4 +172,4 @@ protected function loadTypes()
135172
protected function loadTypeGuesser()
136173
{
137174
}
138-
}
175+
}

0 commit comments

Comments
 (0)