Skip to content

Commit

Permalink
Fix postLink() & postButton() with nested data.
Browse files Browse the repository at this point in the history
Flatten deeply nested array data before generating hidden inputs.
This solves 'Array to string conversion' errors.

Closes #2894
  • Loading branch information
markstory committed Feb 25, 2014
1 parent e0e8f91 commit 0776b87
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
38 changes: 38 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -7430,6 +7430,25 @@ public function testPostButton() {
$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.
*
Expand Down Expand Up @@ -7551,6 +7570,25 @@ public function testPostLink() {
));
}

/**
* 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.
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1715,7 +1715,7 @@ public function button($title, $options = array()) {
public function postButton($title, $url, $options = array()) {
$out = $this->create(false, array('id' => false, 'url' => $url));
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));
}
unset($options['data']);
Expand Down Expand Up @@ -1779,7 +1779,7 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag

$fields = array();
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;
$out .= $this->hidden($key, array('value' => $value, 'id' => false));
}
Expand Down

0 comments on commit 0776b87

Please sign in to comment.