Permalink
Browse files

FIX: NumericField should work with numbers like 54,6

Fixes http://open.silverstripe.org/ticket/5577.

Uses Zend_Locale_Format::isNumber(). Includes unit test for NumericField. Does not include testing work on DBField underlying NumericField to ensure that works consistently.
  • Loading branch information...
1 parent d6733ca commit a99dbae01231d5568e9b3760fdd60a19542bd83e @wilr wilr committed May 11, 2013
Showing with 55 additions and 7 deletions.
  1. +18 −7 forms/NumericField.php
  2. +37 −0 tests/forms/NumericFieldTest.php
View
@@ -1,19 +1,29 @@
<?php
+
/**
- * Text input field with validation for numeric values.
+ * Text input field with validation for numeric values. Supports validating
+ * the numeric value as to the {@link i18n::get_locale()} value.
*
* @package forms
* @subpackage fields-formattedinput
*/
-class NumericField extends TextField{
+class NumericField extends TextField {
public function Type() {
return 'numeric text';
}
- /** PHP Validation **/
- public function validate($validator){
- if($this->value && !is_numeric(trim($this->value))){
+ public function validate($validator) {
+ if(!$this->value && !$validator->fieldIsRequired($this->name)) {
+ return true;
+ }
+
+ $valid = Zend_Locale_Format::isNumber(
@TheFrozenFire

TheFrozenFire May 13, 2013

Contributor

PHP Fatal error: Class 'Zend_Locale_Format' not found in framework/forms/NumericField.php on line 21

@chillu

chillu May 14, 2013

Owner

You fixed this with 644fa29, thanks!

@wilr

wilr May 14, 2013

Owner

Sorry team, scary that my environment didn't pick that up! Must have had a datefield on the page as well.

+ trim($this->value),
+ array('locale' => i18n::get_locale())
+ );
+
+ if(!$valid) {
$validator->validationError(
$this->name,
_t(
@@ -22,10 +32,11 @@ public function validate($validator){
),
"validation"
);
+
return false;
- } else{
- return true;
}
+
+ return true;
}
public function dataValue() {
@@ -0,0 +1,37 @@
+<?php
+
+/**
+ * @package framework
+ * @subpackage tests
+ */
+class NumericFieldTest extends SapphireTest {
+
+ protected $usesDatabase = false;
+
+ public function testValidator() {
+ i18n::set_locale('en_US');
+
+ $field = new NumericField('Number');
+ $field->setValue('12.00');
+
+ $validator = new RequiredFields('Number');
+ $this->assertTrue($field->validate($validator));
+
+ $field->setValue('12,00');
+ $this->assertFalse($field->validate($validator));
+
+ $field->setValue('0');
+ $this->assertTrue($field->validate($validator));
+
+ $field->setValue(false);
+ $this->assertFalse($field->validate($validator));
+
+ i18n::set_locale('de_DE');
+ $field->setValue('12,00');
+ $validator = new RequiredFields();
+ $this->assertTrue($field->validate($validator));
+
+ $field->setValue('12.00');
+ $this->assertFalse($field->validate($validator));
+ }
+}

0 comments on commit a99dbae

Please sign in to comment.