From 9d657ca3e0fcdbd85bf7cc846d973d4d4038d024 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 27 Feb 2014 16:49:09 +0100 Subject: [PATCH] Adding test to prove that non-conventional primaryKeys and composite keys --- src/View/Helper/FormHelper.php | 7 +++-- tests/TestCase/View/Helper/FormHelperTest.php | 29 +++++++++++-------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index dc844f1be3a..b0204038288 100755 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -848,7 +848,9 @@ public function input($fieldName, $options = []) { $this->templates($options['templates']); unset($options['templates']); + $input = $this->_getInput($fieldName, $options); $label = $this->_getLabel($fieldName, $options); + if ($options['type'] !== 'radio') { unset($options['label']); } @@ -862,7 +864,6 @@ public function input($fieldName, $options = []) { } $groupTemplate = $options['type'] === 'checkbox' ? 'checkboxFormGroup' : 'formGroup'; - $input = $this->_getInput($fieldName, $options); $result = $this->formatTemplate($groupTemplate, compact('input', 'label')); if ($options['type'] !== 'hidden') { @@ -1003,7 +1004,7 @@ protected function _optionsOptions($fieldName, $options) { protected function _magicOptions($fieldName, $options, $allowOverride) { $context = $this->_getContext(); - if (!isset($options['required'])) { + if (!isset($options['required']) && $options['type'] !== 'hidden') { $options['required'] = $context->isRequired($fieldName); } @@ -1055,7 +1056,7 @@ protected function _magicOptions($fieldName, $options, $allowOverride) { * @return boolean|string false or Generated label element */ protected function _getLabel($fieldName, $options) { - if ($options['type'] === 'radio') { + if (in_array($options['type'], ['radio', 'hidden'])) { return false; } diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index ebd4b509702..bb5a0ff06c3 100755 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -2914,20 +2914,25 @@ public function testInputSelectType() { * @return void */ public function testInputWithNonStandardPrimaryKeyMakesHidden() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('User'); - $this->Form->fieldset = array( - 'User' => array( - 'fields' => array( - 'model_id' => array('type' => 'integer') - ), - 'validates' => array(), - 'key' => 'model_id' - ) + $this->article['schema']['_constraints']['primary']['columns'] = ['title']; + $this->Form->create($this->article); + $result = $this->Form->input('title'); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'), + ); + $this->assertTags($result, $expected); + + $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body']; + $this->Form->create($this->article); + $result = $this->Form->input('title'); + $expected = array( + 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'), ); - $result = $this->Form->input('model_id'); + $this->assertTags($result, $expected); + + $result = $this->Form->input('body'); $expected = array( - 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'), + 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'), ); $this->assertTags($result, $expected); }