From 0776b8721456f0db6ec5f2f32ecb0fff155293c6 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 24 Feb 2014 21:17:31 -0500 Subject: [PATCH] Fix postLink() & postButton() with nested data. Flatten deeply nested array data before generating hidden inputs. This solves 'Array to string conversion' errors. Closes #2894 --- .../Test/Case/View/Helper/FormHelperTest.php | 38 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 4 +- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index cb96bfeee..9d436133b 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -7430,6 +7430,25 @@ class FormHelperTest extends CakeTestCase { $this->assertTrue(strpos($result, '') !== 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('assertContains('assertContains(' array( + 'two' => array( + 3, 4, 5 + ) + ) + ); + $result = $this->Form->postLink('Send', '/', array('data' => $data)); + $this->assertContains('assertContains('assertContains('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 @@ class FormHelper extends AppHelper { $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)); }