Permalink
Browse files

Rendering a proper value for html5 attribute 'step'

  • Loading branch information...
1 parent c1ab6fa commit 43604f64d57efab73aa6b25362a32a863085e60a @wnasich wnasich committed Jan 30, 2014
Showing with 29 additions and 1 deletion.
  1. +24 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +5 −1 lib/Cake/View/Helper/FormHelper.php
@@ -353,6 +353,8 @@ 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'),
+ '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' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
@@ -1916,6 +1918,28 @@ public function testInput() {
);
$this->assertTags($result, $expected);
+ $result = $this->Form->input('ValidateUser.ratio');
+ $expected = array(
+ 'div' => array('class'),
+ 'label' => array('for'),
+ 'Ratio',
+ '/label',
+ 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
+ '/div',
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('ValidateUser.population');
+ $expected = array(
+ 'div' => array('class'),
+ 'label' => array('for'),
+ 'Population',
+ '/label',
+ 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
+ '/div',
+ );
+ $this->assertTags($result, $expected);
+
$result = $this->Form->input('Contact.email', array('id' => 'custom'));
$expected = array(
'div' => array('class' => 'input email'),
@@ -1173,7 +1173,11 @@ protected function _magicOptions($options) {
!isset($options['step'])
) {
if ($type === 'decimal') {
- $options['step'] = pow(10, -1 * substr($fieldDef['length'], strpos($fieldDef['length'], ',') + 1));
+ if (preg_match('/[0-9]+,([0-9]+)/', $fieldDef['length'], $fieldLenght) && intval($fieldLenght[1]) > 0) {
+ $options['step'] = '0.' . str_repeat('0', $fieldLenght[1] - 1) . '1';
+ } else {
+ $options['step'] = 1;
+ }
} elseif ($type === 'float') {
$options['step'] = 'any';
}

0 comments on commit 43604f6

Please sign in to comment.