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
This commit is contained in:
the_undefined 2008-05-23 05:14:57 +00:00
parent 7402680cc1
commit 52bae8a7bf
2 changed files with 29 additions and 8 deletions

View file

@ -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 = '';

View file

@ -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 &amp; World &gt; weird chars', 'id' => 'ContactPhone')),
array('input' => array('type' => 'text', 'name' => 'data[Contact][phone]', 'value' => 'Hello &amp; World &gt; weird chars', 'id' => 'ContactPhone', 'maxlength' => 255)),
'/div'
);
$this->assertTags($result, $expected);