Skip to content

Commit

Permalink
Merge pull request #14 from hason/keytype
Browse files Browse the repository at this point in the history
Added key_type option
  • Loading branch information
Burgov committed Sep 10, 2015
2 parents 785962c + dfc2a9c commit f522503
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Form/Type/KeyValueRowType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ class KeyValueRowType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (null === $options['allowed_keys']) {
$builder->add('key', 'text', $options['key_options']
);
$builder->add('key', $options['key_type'], $options['key_options']);
} else {
$builder->add('key', 'choice', array_merge(array(
'choice_list' => new SimpleChoiceList($options['allowed_keys'])
Expand All @@ -33,6 +32,7 @@ public function getName()
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'key_type' => 'text',
'key_options' => array(),
'value_options' => array(),
'allowed_keys' => null
Expand All @@ -41,4 +41,4 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setRequired(array('value_type'));
$resolver->setAllowedTypes(array('allowed_keys' => array('null', 'array')));
}
}
}
4 changes: 3 additions & 1 deletion Form/Type/KeyValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'type' => 'burgov_key_value_row',
'allow_add' => true,
'allow_delete' => true,
'key_type' => 'text',
'key_options' => array(),
'value_options' => array(),
'allowed_keys' => null,
'use_container_object' => false,
'options' => function(Options $options) {
return array(
'key_type' => $options['key_type'],
'value_type' => $options['value_type'],
'key_options' => $options['key_options'],
'value_options' => $options['value_options'],
Expand All @@ -69,4 +71,4 @@ public function getName()
{
return 'burgov_key_value';
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The type adds four options to the collection type options, of which one is requi

* `value_type` (required) defines which form type to use to render the value field
* `value_options` optional options to the child defined in `value_type`
* `key_type` defines which form type to use to render the key field (default is a `text` field)
* `key_options` optional options to the child defined in `key_type`
* `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.
* `use_container_object` see explanation below at 'The KeyValueCollection'

Expand Down
55 changes: 46 additions & 9 deletions Tests/Form/Type/KeyValueTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function testSubmitValidData()

$form = $builder->getForm();

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

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

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

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

$form = $builder->getForm();

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

$form->submit(array(
Expand All @@ -96,19 +96,56 @@ public function testWithChoiceType()
)
));

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

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

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

private function assertFormTypes(array $types, $form)
public function testWithCustomKeyType()
{
$this->assertCount(count($types), $form);
foreach ($types as $key => $type) {
$this->assertEquals($type, $form->get($key)->get('value')->getConfig()->getType()->getInnerType()->getName());
$builder = $this->factory->createBuilder('burgov_key_value', null, array(
'key_type' => 'country',
'value_type' => 'integer',
'key_options' => array('label' => 'label_key'),
));

$form = $builder->getForm();

$this->assertFormTypes(array(), array(), $form);
$this->assertFormOptions(array(array('label' => 'label_key'), array()), $form);

$form->submit(array(
array(
'key' => 'GB',
'value' => '2'
),
array(
'key' => 'CZ',
'value' => '1'
)
));

$this->assertFormTypes(array('country', 'country'), array('integer', 'integer'), $form);
$this->assertFormOptions(array(array('label' => 'label_key'), array()), $form);

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

$this->assertSame(array('GB' => 2, 'CZ' => 1), $form->getData());
}

private function assertFormTypes(array $keys, array $values, $form)
{
$this->assertCount(count($values), $form);
for ($i = 0; $i < count($form); $i++) {
if (isset($keys[$i])) {
$this->assertEquals($keys[$i], $form->get($i)->get('key')->getConfig()->getType()->getInnerType()->getName());
}
if (isset($values[$i])) {
$this->assertEquals($values[$i], $form->get($i)->get('value')->getConfig()->getType()->getInnerType()->getName());
}
}
}

Expand All @@ -135,4 +172,4 @@ protected function loadTypes()
protected function loadTypeGuesser()
{
}
}
}

0 comments on commit f522503

Please sign in to comment.