Skip to content

Commit

Permalink
Fixing issues with HTML5 number inputs and float columns.
Browse files Browse the repository at this point in the history
For float columns, the default step value is 'any'
Fixes #1907
  • Loading branch information
markstory committed Aug 19, 2011
1 parent 0e21093 commit 46c07ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -859,7 +859,7 @@ public function testFormSecurityFields() {
} }


/** /**
* Tests correct generation of text fields for double and float fields * Tests correct generation of number fields for double and float fields
* *
* @access public * @access public
* @return void * @return void
Expand All @@ -876,20 +876,39 @@ public function testTextFieldGenerationForFloats() {
$this->Form->create('Contact'); $this->Form->create('Contact');
$result = $this->Form->input('foo'); $result = $this->Form->input('foo');
$expected = array( $expected = array(
'div' => array('class' => 'input text'), 'div' => array('class' => 'input number'),
'label' => array('for' => 'ContactFoo'), 'label' => array('for' => 'ContactFoo'),
'Foo', 'Foo',
'/label', '/label',
array('input' => array( array('input' => array(
'type' => 'text', 'name' => 'data[Contact][foo]', 'type' => 'number',
'id' => 'ContactFoo' 'name' => 'data[Contact][foo]',
'id' => 'ContactFoo',
'step' => 'any'
)), )),
'/div' '/div'
); );
$this->assertTags($result, $expected);

$result = $this->Form->input('foo', array('step' => 0.5));
$expected = array(
'div' => array('class' => 'input number'),
'label' => array('for' => 'ContactFoo'),
'Foo',
'/label',
array('input' => array(
'type' => 'number',
'name' => 'data[Contact][foo]',
'id' => 'ContactFoo',
'step' => '0.5'
)),
'/div'
);
$this->assertTags($result, $expected);
} }


/** /**
* Tests correct generation of text fields for double and float fields * Tests correct generation of number fields for integer fields
* *
* @access public * @access public
* @return void * @return void
Expand All @@ -906,7 +925,7 @@ public function testTextFieldTypeNumberGenerationForIntegers() {
$this->Form->create('Contact'); $this->Form->create('Contact');
$result = $this->Form->input('foo'); $result = $this->Form->input('foo');
$expected = array( $expected = array(
'div' => array('class' => 'input text'), 'div' => array('class' => 'input number'),
'label' => array('for' => 'ContactFoo'), 'label' => array('for' => 'ContactFoo'),
'Foo', 'Foo',
'/label', '/label',
Expand All @@ -916,6 +935,7 @@ public function testTextFieldTypeNumberGenerationForIntegers() {
)), )),
'/div' '/div'
); );
$this->assertTags($result, $expected);
} }


/** /**
Expand Down
7 changes: 7 additions & 0 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -939,6 +939,13 @@ public function input($fieldName, $options = array()) {
if ($fieldKey == $primaryKey) { if ($fieldKey == $primaryKey) {
$options['type'] = 'hidden'; $options['type'] = 'hidden';
} }
if (
$options['type'] === 'number' &&
$type === 'float' &&
!isset($options['step'])
) {
$options['step'] = 'any';
}
} }
if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') { if (preg_match('/_id$/', $fieldKey) && $options['type'] !== 'hidden') {
$options['type'] = 'select'; $options['type'] = 'select';
Expand Down

0 comments on commit 46c07ad

Please sign in to comment.