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
This commit is contained in:
mark_story 2014-03-18 22:11:57 -04:00
parent 23350eb89e
commit ee895a8bb1
2 changed files with 28 additions and 12 deletions

View file

@ -3614,18 +3614,22 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $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( $expected = array(
'fieldset' => array(), 'fieldset' => array(),
'legend' => array(), 'legend' => array(),
'Gender', 'Gender',
'/legend', '/legend',
'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'), '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')), array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
array('label' => array('for' => 'EmployeeGenderMale')), array('label' => array('for' => 'EmployeeGenderMale')),
'Male', 'Male',
'/label', '/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')), array('label' => array('for' => 'EmployeeGenderFemale')),
'Female', 'Female',
'/label', '/label',
@ -4650,15 +4654,18 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Form->select( $result = $this->Form->select(
'Model.multi_field', $options, array('multiple' => 'multiple') 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
); );
$expected = array( $expected = array(
'input' => 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( 'select' => array(
'name' => 'data[Model][multi_field][]', 'name' => 'data[Model][multi_field][]',
'id' => 'ModelMultiField', 'multiple' => 'multiple' 'id' => 'ModelMultiField',
'multiple' => 'multiple',
'form' => 'my-form',
), ),
array('option' => array('value' => '0')), array('option' => array('value' => '0')),
'first', 'first',
@ -5674,10 +5681,14 @@ class FormHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $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( $expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', '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')) array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);

View file

@ -1418,7 +1418,8 @@ class FormHelper extends AppHelper {
'id' => $options['id'] . '_', 'id' => $options['id'] . '_',
'name' => $options['name'], 'name' => $options['name'],
'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'), 'value' => ($options['hiddenField'] !== true ? $options['hiddenField'] : '0'),
'secure' => false 'form' => isset($options['form']) ? $options['form'] : null,
'secure' => false,
); );
if (isset($options['disabled']) && $options['disabled']) { if (isset($options['disabled']) && $options['disabled']) {
$hiddenOptions['disabled'] = 'disabled'; $hiddenOptions['disabled'] = 'disabled';
@ -1544,7 +1545,10 @@ class FormHelper extends AppHelper {
if ($hiddenField) { if ($hiddenField) {
if (!isset($value) || $value === '') { if (!isset($value) || $value === '') {
$hidden = $this->hidden($fieldName, array( $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' => '', 'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'), 'id' => $attributes['id'] . ($style ? '' : '_'),
'secure' => false, 'secure' => false,
'form' => isset($attributes['form']) ? $attributes['form'] : null,
'name' => $attributes['name'] 'name' => $attributes['name']
); );
$select[] = $this->hidden(null, $hiddenAttributes); $select[] = $this->hidden(null, $hiddenAttributes);