Fix maxlength for manual type set and add textarea maxlength support.

This commit is contained in:
euromark 2014-09-13 04:03:33 +02:00
parent 3fb252ad2f
commit 03d92494f1
2 changed files with 42 additions and 7 deletions

View file

@ -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()),

View file

@ -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'];
}