From ee895a8bb1edeb93c548cc17e4141b453004cef2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Tue, 18 Mar 2014 22:11:57 -0400 Subject: [PATCH] Add form attribute to hidden inputs when present. If inputs are placed outside of the form elements the form attribute needs to be set on the hidden inputs. Without this attribute the empty state does not submit correctly. Fixes #3053 --- .../Test/Case/View/Helper/FormHelperTest.php | 31 +++++++++++++------ lib/Cake/View/Helper/FormHelper.php | 9 ++++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 0786c6007..4a7ba4946 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -3614,18 +3614,22 @@ class FormHelperTest extends CakeTestCase { ); $this->assertTags($result, $expected); - $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female')); + $result = $this->Form->radio( + 'Employee.gender', + array('male' => 'Male', 'female' => 'Female'), + array('form' => 'my-form') + ); $expected = array( 'fieldset' => array(), 'legend' => array(), 'Gender', '/legend', - 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'), - array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')), + 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'), + array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')), array('label' => array('for' => 'EmployeeGenderMale')), 'Male', '/label', - array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')), + array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')), array('label' => array('for' => 'EmployeeGenderFemale')), 'Female', '/label', @@ -4650,15 +4654,18 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); $result = $this->Form->select( - 'Model.multi_field', $options, array('multiple' => 'multiple') + 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple') ); $expected = array( 'input' => array( - 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_' + 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_', + 'form' => 'my-form', ), 'select' => array( 'name' => 'data[Model][multi_field][]', - 'id' => 'ModelMultiField', 'multiple' => 'multiple' + 'id' => 'ModelMultiField', + 'multiple' => 'multiple', + 'form' => 'my-form', ), array('option' => array('value' => '0')), 'first', @@ -5674,10 +5681,14 @@ class FormHelperTest extends CakeTestCase { ); $this->assertTags($result, $expected); - $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue')); + $result = $this->Form->checkbox('Model.field', array( + 'id' => 'theID', + 'value' => 'myvalue', + 'form' => 'my-form', + )); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'), - array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID')) + 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'), + array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form')) ); $this->assertTags($result, $expected); diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 4f7bf48da..4a532003b 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1418,7 +1418,8 @@ class FormHelper extends AppHelper { 'id' => $options['id'] . '_', 'name' => $options['name'], 'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'), - 'secure' => false + 'form' => isset($options['form']) ? $options['form'] : null, + 'secure' => false, ); if (isset($options['disabled']) && $options['disabled']) { $hiddenOptions['disabled'] = 'disabled'; @@ -1544,7 +1545,10 @@ class FormHelper extends AppHelper { if ($hiddenField) { if (!isset($value) || $value === '') { $hidden = $this->hidden($fieldName, array( - 'id' => $attributes['id'] . '_', 'value' => '', 'name' => $attributes['name'] + 'form' => isset($attributes['form']) ? $attributes['form'] : null, + 'id' => $attributes['id'] . '_', + 'value' => '', + 'name' => $attributes['name'] )); } } @@ -2008,6 +2012,7 @@ class FormHelper extends AppHelper { 'value' => '', 'id' => $attributes['id'] . ($style ? '' : '_'), 'secure' => false, + 'form' => isset($attributes['form']) ? $attributes['form'] : null, 'name' => $attributes['name'] ); $select[] = $this->hidden(null, $hiddenAttributes);