diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 3efae92d00d..b6c1771dc35 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -1539,7 +1539,7 @@ public function radio($fieldName, $options = array(), $attributes = array()) { * @throws Cake\Error\Exception When there are no params for the method call. */ public function __call($method, $params) { - $options = array(); + $options = []; if (empty($params)) { throw new Error\Exception(sprintf('Missing field name for FormHelper::%s', $method)); } @@ -1550,7 +1550,7 @@ public function __call($method, $params) { $options['type'] = $method; } $options = $this->_initInputField($params[0], $options); - return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null))); + return $this->widget($options['type'], $options); } /** @@ -2861,6 +2861,7 @@ protected function _initInputField($field, $options = []) { $first = array_shift($parts); $options['name'] = $first . ($parts ? '[' . implode('][', $parts) . ']' : ''); } + if (isset($options['value']) && !isset($options['val'])) { $options['val'] = $options['value']; unset($options['value']); @@ -2868,6 +2869,11 @@ protected function _initInputField($field, $options = []) { if (!isset($options['val'])) { $options['val'] = $context->val($field); } + if (!isset($options['val']) && isset($options['default'])) { + $options['val'] = $options['default']; + } + unset($options['default']); + $options += (array)$context->attributes($field); if ($context->hasError($field)) { diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 5ff81f64cdb..bf7f2d56b3f 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -1280,12 +1280,14 @@ public function testFormSecurityFields() { */ public function testTextFieldGenerationForFloats() { $this->markTestIncomplete('Need to revisit once models work again.'); - $model->setSchema(array('foo' => array( - 'type' => 'float', - 'null' => false, - 'default' => null, - 'length' => 10 - ))); + $this->article['schema'] = [ + 'foo' => [ + 'type' => 'float', + 'null' => false, + 'default' => null, + 'length' => 10 + ] + ]; $this->Form->create('Contact'); $result = $this->Form->input('foo'); @@ -3299,10 +3301,9 @@ public function testInputCheckboxWithDisabledElements() { * @return void */ public function testInputWithLeadingInteger() { - $this->markTestIncomplete('Need to revisit once models work again.'); $result = $this->Form->text('0.Node.title'); $expected = array( - 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text') + 'input' => array('name' => '0[Node][title]', 'type' => 'text') ); $this->assertTags($result, $expected); } @@ -3990,30 +3991,49 @@ public function testLabel() { * @return void */ public function testTextbox() { - $this->markTestIncomplete('Need to revisit once models work again.'); $result = $this->Form->text('Model.field'); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField'))); + $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]'))); $result = $this->Form->text('Model.field', array('type' => 'password')); - $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField'))); + $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]'))); $result = $this->Form->text('Model.field', array('id' => 'theID')); $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'))); + } + +/** + * Test that text() hooks up with request data and error fields. + * + * @return void + */ + public function testTextBoxDataAndError() { + $this->article['errors'] = [ + 'Contact' => ['text' => 'wrong'] + ]; + $this->Form->create($this->article); $this->Form->request->data['Model']['text'] = 'test HTML values'; $result = $this->Form->text('Model.text'); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test <strong>HTML</strong> values', 'id' => 'ModelText'))); + $expected = [ + 'input' => [ + 'type' => 'text', + 'name' => 'Model[text]', + 'value' => 'test <strong>HTML</strong> values', + ] + ]; + $this->assertTags($result, $expected); - $Contact->validationErrors['text'] = array(true); $this->Form->request->data['Contact']['text'] = 'test'; - $result = $this->Form->text('Contact.text', array('id' => 'theID')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'))); - - $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value'; - $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId')); - $expected = array( - 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId') - ); + $result = $this->Form->text('Contact.text', ['id' => 'theID']); + $expected = [ + 'input' => [ + 'type' => 'text', + 'name' => 'Contact[text]', + 'value' => 'test', + 'id' => 'theID', + 'class' => 'form-error' + ] + ]; $this->assertTags($result, $expected); } @@ -4024,15 +4044,16 @@ public function testTextbox() { * * @return void */ - public function testDefaultValue() { - $this->markTestIncomplete('Need to revisit once models work again.'); + public function testTextDefaultValue() { $this->Form->request->data['Model']['field'] = 'test'; $result = $this->Form->text('Model.field', array('default' => 'default value')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField'))); + $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']]; + $this->assertTags($result, $expected); unset($this->Form->request->data['Model']['field']); $result = $this->Form->text('Model.field', array('default' => 'default value')); - $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField'))); + $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']]; + $this->assertTags($result, $expected); } /** @@ -4190,11 +4211,16 @@ public function testInputErrorEscape() { * @return void */ public function testPassword() { - $this->markTestIncomplete('Need to revisit once models work again.'); + $this->article['errors'] = [ + 'Contact' => [ + 'passwd' => 1 + ] + ]; + $this->Form->create($this->article); + $result = $this->Form->password('Contact.field'); - $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField'))); + $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]'))); - $Contact->validationErrors['passwd'] = 1; $this->Form->request->data['Contact']['passwd'] = 'test'; $result = $this->Form->password('Contact.passwd', array('id' => 'theID')); $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));