Permalink
Browse files

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

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 0776b8721456f0db6ec5f2f32ecb0fff155293c6
Showing with 40 additions and 2 deletions.
  1. +38 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +2 −2 lib/Cake/View/Helper/FormHelper.php
@@ -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.
*
@@ -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.
*
@@ -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']);
@@ -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));
}

0 comments on commit 0776b87

Please sign in to comment.