Fix postLink() & postButton() with nested data.

Flatten deeply nested array data before generating hidden inputs.
This solves 'Array to string conversion' errors.

Closes #2894
This commit is contained in:
mark_story 2014-02-24 21:17:31 -05:00
parent e0e8f91d9e
commit 0776b87214
2 changed files with 40 additions and 2 deletions

View file

@ -7430,6 +7430,25 @@ class FormHelperTest extends CakeTestCase {
$this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false); $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
} }
/**
* Test using postButton with N dimensional data.
*
* @return void
*/
public function testPostButtonNestedData() {
$data = array(
'one' => array(
'two' => array(
3, 4, 5
)
)
);
$result = $this->Form->postButton('Send', '/', array('data' => $data));
$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
}
/** /**
* Test that postButton adds _Token fields. * Test that postButton adds _Token fields.
* *
@ -7551,6 +7570,25 @@ class FormHelperTest extends CakeTestCase {
)); ));
} }
/**
* Test using postLink with N dimensional data.
*
* @return void
*/
public function testPostLinkNestedData() {
$data = array(
'one' => array(
'two' => array(
3, 4, 5
)
)
);
$result = $this->Form->postLink('Send', '/', array('data' => $data));
$this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
$this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
$this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
}
/** /**
* test creating postLinks after a GET form. * test creating postLinks after a GET form.
* *

View file

@ -1715,7 +1715,7 @@ class FormHelper extends AppHelper {
public function postButton($title, $url, $options = array()) { public function postButton($title, $url, $options = array()) {
$out = $this->create(false, array('id' => false, 'url' => $url)); $out = $this->create(false, array('id' => false, 'url' => $url));
if (isset($options['data']) && is_array($options['data'])) { if (isset($options['data']) && is_array($options['data'])) {
foreach ($options['data'] as $key => $value) { foreach (Hash::flatten($options['data']) as $key => $value) {
$out .= $this->hidden($key, array('value' => $value, 'id' => false)); $out .= $this->hidden($key, array('value' => $value, 'id' => false));
} }
unset($options['data']); unset($options['data']);
@ -1779,7 +1779,7 @@ class FormHelper extends AppHelper {
$fields = array(); $fields = array();
if (isset($options['data']) && is_array($options['data'])) { if (isset($options['data']) && is_array($options['data'])) {
foreach ($options['data'] as $key => $value) { foreach (Hash::flatten($options['data']) as $key => $value) {
$fields[$key] = $value; $fields[$key] = $value;
$out .= $this->hidden($key, array('value' => $value, 'id' => false)); $out .= $this->hidden($key, array('value' => $value, 'id' => false));
} }