mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #1877 from wilr/5577
FIX: NumericField should work with numbers like 54,6
This commit is contained in:
commit
46bc588a1b
@ -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(
|
||||
trim($this->value),
|
||||
array('locale' => i18n::get_locale())
|
||||
);
|
||||
|
||||
if(!$valid) {
|
||||
$validator->validationError(
|
||||
$this->name,
|
||||
_t(
|
||||
@ -22,10 +32,11 @@ class NumericField extends TextField{
|
||||
),
|
||||
"validation"
|
||||
);
|
||||
|
||||
return false;
|
||||
} else{
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dataValue() {
|
||||
|
37
tests/forms/NumericFieldTest.php
Normal file
37
tests/forms/NumericFieldTest.php
Normal file
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user