From 1759d5d017068693b20f82ac7009004fd0163375 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 4 Aug 2014 14:20:57 +1200 Subject: [PATCH] API Use "number" HTML5 type for NumericField by default --- docs/en/changelogs/3.2.0.md | 1 + forms/NumericField.php | 7 +++++++ tests/forms/NumericFieldTest.php | 11 ++++++++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/en/changelogs/3.2.0.md b/docs/en/changelogs/3.2.0.md index 45decc2a7..b9e31c6e2 100644 --- a/docs/en/changelogs/3.2.0.md +++ b/docs/en/changelogs/3.2.0.md @@ -6,6 +6,7 @@ * Minimum PHP version raised to 5.3.3 * DataObject::validate() method visibility changed to public + * NumericField now uses HTML5 "number" type instead of "text" * UploadField "Select from files" shows files in all folders by default * UploadField won't display an overwrite warning unless Upload:replaceFile is true * HtmlEditorField no longer substitutes `
` for indented text diff --git a/forms/NumericField.php b/forms/NumericField.php index 18e361037..41d9c6d40 100644 --- a/forms/NumericField.php +++ b/forms/NumericField.php @@ -13,6 +13,13 @@ class NumericField extends TextField { return 'numeric text'; } + public function getAttributes() { + return array_merge(parent::getAttributes(), array( + 'type' => 'number', + 'step' => 'any' // allows entering float/decimal numbers like "1.2" instead of just integers + )); + } + public function validate($validator) { if(!$this->value && !$validator->fieldIsRequired($this->name)) { return true; diff --git a/tests/forms/NumericFieldTest.php b/tests/forms/NumericFieldTest.php index bd55c0ae6..9642205e4 100644 --- a/tests/forms/NumericFieldTest.php +++ b/tests/forms/NumericFieldTest.php @@ -34,4 +34,13 @@ class NumericFieldTest extends SapphireTest { $field->setValue('12.00'); $this->assertFalse($field->validate($validator)); } -} \ No newline at end of file + + public function testNumberTypeOnInputHtml() { + $field = new NumericField('Number'); + + $html = $field->Field(); + $this->assertContains('type="number"', $html, 'number type set'); + $this->assertContains('step="any"', $html, 'step value set to any'); + } + +}