From 03d92494f132df6ff6781ee6160ffe8405d25162 Mon Sep 17 00:00:00 2001 From: euromark Date: Sat, 13 Sep 2014 04:03:33 +0200 Subject: [PATCH] Fix maxlength for manual type set and add textarea maxlength support. --- .../Test/Case/View/Helper/FormHelperTest.php | 42 ++++++++++++++++--- lib/Cake/View/Helper/FormHelper.php | 7 +++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index d9f69255c..2d71d88c2 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -2046,7 +2046,7 @@ class FormHelperTest extends CakeTestCase { 'Email', '/label', array('input' => array( - 'type' => 'text', 'name' => 'data[Contact][email]', + 'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]', 'id' => 'ContactEmail' )), '/div' @@ -2060,7 +2060,7 @@ class FormHelperTest extends CakeTestCase { 'Email', '/label', array('input' => array( - 'type' => 'text', 'name' => 'data[Contact][5][email]', + 'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][5][email]', 'id' => 'Contact5Email' )), '/div' @@ -7464,6 +7464,37 @@ class FormHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * Test textareas waxlength read from schema. + * + * @return void + */ + public function testTextAreaMaxLength() { + $result = $this->Form->input('UserForm.other', array('type' => 'textarea')); + $expected = array( + 'div' => array('class' => 'input textarea'), + 'label' => array('for' => 'UserFormOther'), + 'Other', + '/label', + 'textarea' => array('name' => 'data[UserForm][other]', 'cols' => '30', 'rows' => '6', 'id' => 'UserFormOther'), + '/textarea', + '/div' + ); + $this->assertTags($result, $expected); + + $result = $this->Form->input('UserForm.stuff', array('type' => 'textarea')); + $expected = array( + 'div' => array('class' => 'input textarea'), + 'label' => array('for' => 'UserFormStuff'), + 'Stuff', + '/label', + 'textarea' => array('name' => 'data[UserForm][stuff]', 'maxlength' => 10, 'cols' => '30', 'rows' => '6', 'id' => 'UserFormStuff'), + '/textarea', + '/div' + ); + $this->assertTags($result, $expected); + } + /** * testTextAreaWithStupidCharacters method * @@ -9586,6 +9617,7 @@ class FormHelperTest extends CakeTestCase { 'textarea' => array( 'id' => 'ValidateProfile1ValidateItem2Name', 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]', + 'maxlength' => 255, 'cols' => 30, 'rows' => 6 ), @@ -9702,7 +9734,7 @@ class FormHelperTest extends CakeTestCase { $expected = array( 'div' => array('class' => 'input text'), 'input' => array( - 'type' => 'text', 'name' => 'data[Contact][email]', + 'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]', 'id' => 'ContactEmail' ), '/div' @@ -9716,7 +9748,7 @@ class FormHelperTest extends CakeTestCase { $expected = array( 'div' => array('class' => 'input text'), array('input' => array( - 'type' => 'text', 'name' => 'data[Contact][email]', + 'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]', 'id' => 'ContactEmail' )), 'label' => array('for' => 'ContactEmail'), @@ -9736,7 +9768,7 @@ class FormHelperTest extends CakeTestCase { $expected = array( 'div' => array('class' => 'input text'), array('input' => array( - 'type' => 'text', 'name' => 'data[Contact][email]', + 'maxlength' => 255, 'type' => 'text', 'name' => 'data[Contact][email]', 'id' => 'ContactEmail' )), array('div' => array()), diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 929ac13d6..0dd2e1380 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1125,6 +1125,8 @@ class FormHelper extends AppHelper { $options = $this->_optionsOptions($options); } + $options = $this->_maxLength($options); + if (isset($options['rows']) || isset($options['cols'])) { $options['type'] = 'textarea'; } @@ -1228,7 +1230,7 @@ class FormHelper extends AppHelper { if ($options['type'] === 'select' && array_key_exists('step', $options)) { unset($options['step']); } - $options = $this->_maxLength($options); + return $options; } @@ -1286,10 +1288,11 @@ class FormHelper extends AppHelper { !array_key_exists('maxlength', $options) && isset($fieldDef['length']) && is_scalar($fieldDef['length']) && + $fieldDef['length'] < 1000000 && $options['type'] !== 'select' ); if ($autoLength && - in_array($options['type'], array('text', 'email', 'tel', 'url', 'search')) + in_array($options['type'], array('text', 'textarea', 'email', 'tel', 'url', 'search')) ) { $options['maxlength'] = $fieldDef['length']; }