Skip to content

Commit

Permalink
Fixed reverseTransform to ChoiceToJsonTransformer
Browse files Browse the repository at this point in the history
  • Loading branch information
genemu committed Nov 18, 2011
1 parent cd8ca95 commit 604a7a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
7 changes: 1 addition & 6 deletions Form/DataTransformer/ArrayToJsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public function transform($array)
*/
public function reverseTransform($values)
{
$array = array();
foreach (json_decode($values[0], true) as $value) {
$array[] = $value['value'];
}

return $array;
return json_decode($values, true);
}
}
2 changes: 1 addition & 1 deletion Form/DataTransformer/ChoiceToJsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public function transform($choices)
*/
public function reverseTransform($values)
{
return json_decode($values[0], true);
return json_decode($values, true);
}
}
7 changes: 1 addition & 6 deletions Form/DataTransformer/FieldToJsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public function transform($array)
*/
public function reverseTransform($values)
{
$array = array();
foreach (json_decode($values[0], true) as $value) {
$array[] = $value['value'];
}

return $array;
return json_decode($values, true);
}
}
25 changes: 15 additions & 10 deletions Tests/Form/Type/JQueryAutocompleterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function testDefaultConfigs()
$this->assertNull($view->get('route_name'));
}

public function testConfigs()
public function testValue()
{
$form = $this->factory->create('genemu_jqueryautocompleter', null, array(
'choices' => array('foo' => 'Foo', 'bar' => 'Bar')
Expand Down Expand Up @@ -113,17 +113,20 @@ public function testValueWithAjax()

$form->setData(array('foo' => 'Foo'));
$view = $form->createView();

$this->assertEquals(json_encode(array(
'label' => 'Foo', 'value' => 'foo'
)), $form->getClientData());
$form->bind(json_encode(array('bar' => 'Bar')));

$this->assertFalse($form->hasAttribute('choice_list'));
$this->assertEquals(json_encode(array(
'label' => 'Foo', 'value' => 'foo'
)), $view->get('value'));

$this->assertEquals('Foo', $view->get('autocompleter_value'));

$this->assertEquals(array('bar' => 'Bar'), $form->getData());
$this->assertEquals(json_encode(array(
'label' => 'Bar', 'value' => 'bar'
)), $form->getClientData());

}

public function testValueMultipleWithAjax()
Expand All @@ -135,19 +138,19 @@ public function testValueMultipleWithAjax()

$form->setData(array('foo' => 'Foo', 'bar' => 'Bar'));
$view = $form->createView();
$form->bind(json_encode(array('foo' => 'Foo', 'ri' => 'Ri')));

$this->assertEquals(array(), $form->getAttribute('choice_list')->getChoices());
$this->assertEquals(json_encode(array(
array('label' => 'Foo', 'value' => 'foo'),
array('label' => 'Bar', 'value' => 'bar'),
array('label' => 'Ri', 'value' => 'ri'),
)), $form->getClientData());

$this->assertEquals(array(), $form->getAttribute('choice_list')->getChoices());
$this->assertEquals(array('foo' => 'Foo', 'ri' => 'Ri'), $form->getData());

$this->assertEquals(json_encode(array(
array('label' => 'Foo', 'value' => 'foo'),
array('label' => 'Bar', 'value' => 'bar'),
)), $view->get('value'));

$this->assertEquals('Foo, Bar, ', $view->get('autocompleter_value'));
}

Expand All @@ -160,12 +163,14 @@ public function testValueMultiple()

$form->setData(array('foo', 'bar'));
$view = $form->createView();
$form->bind(json_encode(array('foo')));

$this->assertEquals(json_encode(array(
array('label' => 'Foo', 'value' => 'foo'),
array('label' => 'Bar', 'value' => 'bar')
)), $form->getClientData());

$this->assertEquals(array('foo'), $form->getData());

$this->assertEquals(array(
array('label' => 'Foo', 'value' => 'foo'),
array('label' => 'Bar', 'value' => 'bar'),
Expand Down

0 comments on commit 604a7a2

Please sign in to comment.