Skip to content

Commit

Permalink
Correct input generation for postgres numeric types.
Browse files Browse the repository at this point in the history
Numeric types in postgres are treated like decimals, except they can
have no length, precision or scale components defined.

IE does not accept 1.00000 as a valid step attribute so we'll default to
any when we encounter decimal types with no length.

Refs #7497
  • Loading branch information
markstory committed Oct 2, 2015
1 parent 3fa5c4f commit 506051f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -353,6 +353,7 @@ class ValidateUser extends CakeTestModel {
'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
'null_decimal' => array('type' => 'decimal', 'null' => false, 'length' => null),
'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
Expand Down Expand Up @@ -2045,6 +2046,17 @@ public function testInput() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('ValidateUser.null_decimal');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'Null Decimal',
'/label',
'input' => array('name', 'type' => 'number', 'step' => 'any', 'id'),
'/div',
);
$this->assertTags($result, $expected);

$result = $this->Form->input('ValidateUser.ratio');
$expected = array(
'div' => array('class'),
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1209,10 +1209,10 @@ protected function _magicOptions($options) {
if ($options['type'] === 'number' &&
!isset($options['step'])
) {
if ($type === 'decimal') {
if ($type === 'decimal' && isset($fieldDef['length'])) {
$decimalPlaces = substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1);
$options['step'] = sprintf('%.' . $decimalPlaces . 'F', pow(10, -1 * $decimalPlaces));
} elseif ($type === 'float') {
} elseif ($type === 'float' || $type === 'decimal') {
$options['step'] = 'any';
}
}
Expand Down

1 comment on commit 506051f

@pyrite357
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks mark, I'll try to get this tested tomorrow.

Please sign in to comment.