From 52bae8a7bf1985a85e1fee8837d05fd10f949378 Mon Sep 17 00:00:00 2001 From: the_undefined Date: Fri, 23 May 2008 05:14:57 +0000 Subject: [PATCH] Made form helper pick up field definitions outside the current context Made form helper handle maxlength for float fields properly, fixes #4724 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7021 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/form.php | 19 ++++++++++++++----- .../cases/libs/view/helpers/form.test.php | 18 +++++++++++++++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cake/libs/view/helpers/form.php b/cake/libs/view/helpers/form.php index 981e0c571..268e6b256 100644 --- a/cake/libs/view/helpers/form.php +++ b/cake/libs/view/helpers/form.php @@ -541,11 +541,18 @@ class FormHelper extends AppHelper { } elseif (in_array($this->field(), array('psword', 'passwd', 'password'))) { $options['type'] = 'password'; } elseif (isset($this->fieldset['fields'][$this->field()])) { - $type = $this->fieldset['fields'][$this->field()]['type']; + $fieldDef = $this->fieldset['fields'][$this->field()]; + $type = $fieldDef['type']; $primaryKey = $this->fieldset['key']; } elseif (ClassRegistry::isKeySet($this->model())) { $model =& ClassRegistry::getObject($this->model()); $type = $model->getColumnType($this->field()); + $fieldDef = $model->schema(); + if (isset($fieldDef[$this->field()])) { + $fieldDef = $fieldDef[$this->field()]; + } else { + $fieldDef = array(); + } $primaryKey = $model->primaryKey; } @@ -587,10 +594,12 @@ class FormHelper extends AppHelper { } } - if (!array_key_exists('maxlength', $options) && $options['type'] == 'text') { - if (!empty($this->fieldset['fields'][$this->field()]['length'])) { - $options['maxlength'] = $this->fieldset['fields'][$this->field()]['length']; - } + $autoLength = (!array_key_exists('maxlength', $options) && isset($fieldDef['length'])); + if ($autoLength && $options['type'] == 'text') { + $options['maxlength'] = $fieldDef['length']; + } + if ($autoLength && $fieldDef['type'] == 'float') { + $options['maxlength'] = array_sum(explode(',', $fieldDef['length']))+1; } $out = ''; diff --git a/cake/tests/cases/libs/view/helpers/form.test.php b/cake/tests/cases/libs/view/helpers/form.test.php index 61692ec69..986c6b21c 100644 --- a/cake/tests/cases/libs/view/helpers/form.test.php +++ b/cake/tests/cases/libs/view/helpers/form.test.php @@ -142,6 +142,7 @@ class ValidateUser extends CakeTestModel { 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'), 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'), + 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'), 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''), 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null) ); @@ -604,7 +605,7 @@ class FormHelperTest extends CakeTestCase { 'label' => array('for'), 'preg:/[^<]+/', '/label', - 'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error'), + 'input' => array('type' => 'text', 'name', 'value' => '', 'id', 'class' => 'form-error', 'maxlength' => 255), array('div' => array('class' => 'error-message')), 'This field cannot be left blank', '/div', @@ -689,13 +690,24 @@ class FormHelperTest extends CakeTestCase { } function testFormInput() { + $result = $this->Form->input('ValidateUser.balance'); + $expected = array( + 'div' => array('class'), + 'label' => array('for'), + 'Balance', + '/label', + 'input' => array('name', 'type' => 'text', 'maxlength' => 8, 'value' => '', 'id'), + '/div', + ); + $this->assertTags($result, $expected); + $result = $this->Form->input('Contact.email', array('id' => 'custom')); $expected = array( 'div' => array('class' => 'input text'), 'label' => array('for' => 'custom'), 'Email', '/label', - array('input' => array('type' => 'text', 'name' => 'data[Contact][email]', 'value' => '', 'id' => 'custom')), + array('input' => array('type' => 'text', 'name' => 'data[Contact][email]', 'value' => '', 'id' => 'custom', 'maxlength' => 255)), '/div' ); $this->assertTags($result, $expected); @@ -801,7 +813,7 @@ class FormHelperTest extends CakeTestCase { 'label' => array('for' => 'ContactPhone'), 'Phone', '/label', - array('input' => array('type' => 'text', 'name' => 'data[Contact][phone]', 'value' => 'Hello & World > weird chars', 'id' => 'ContactPhone')), + array('input' => array('type' => 'text', 'name' => 'data[Contact][phone]', 'value' => 'Hello & World > weird chars', 'id' => 'ContactPhone', 'maxlength' => 255)), '/div' ); $this->assertTags($result, $expected);