Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Making Validation::decimal accept integers ( #2800 ) #610

Closed
wants to merge 2 commits into from

2 participants

@CauanCabral

Force locale of ValidationTests with en_US to ensure decimal dot separator
Tests updated

CauanCabral added some commits
@CauanCabral CauanCabral Make Validation::decimal accept integers
Fix #2800
Force locale of ValidationTests with en_US to ensure decimal dot separator
890eb21
@CauanCabral CauanCabral Include test to .0 and .00 decimals 9d768b4
@markstory
Owner

Since this issue exists on 2.1 as well, I've cherry-picked the commits onto that branch. They'll end up on 2.2. in the next merge. See [18aa5a0] and 28bd688]

@markstory markstory closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 17, 2012
  1. @CauanCabral

    Make Validation::decimal accept integers

    CauanCabral authored
    Fix #2800
    Force locale of ValidationTests with en_US to ensure decimal dot separator
  2. @CauanCabral
This page is out of date. Refresh to see the latest.
View
15 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -103,6 +103,8 @@ class ValidationTest extends CakeTestCase {
public function setUp() {
parent::setUp();
$this->_appEncoding = Configure::read('App.encoding');
+ $this->_appLocale = setlocale(LC_ALL, "0");
+ setlocale(LC_ALL, 'en_US');
}
/**
@@ -113,6 +115,7 @@ public function setUp() {
public function tearDown() {
parent::tearDown();
Configure::write('App.encoding', $this->_appEncoding);
+ setlocale(LC_ALL, $this->_appLocale);
}
/**
@@ -1490,10 +1493,16 @@ public function testDecimal() {
$this->assertTrue(Validation::decimal('+0123.45e6'));
$this->assertTrue(Validation::decimal('-0123.45e6'));
$this->assertTrue(Validation::decimal('0123.45e6'));
+ $this->assertTrue(Validation::decimal('1234'));
+ $this->assertTrue(Validation::decimal('-1234'));
+ $this->assertTrue(Validation::decimal('+1234'));
+ $this->assertTrue(Validation::decimal(1234.56));
+ $this->assertTrue(Validation::decimal(1234.00));
+ $this->assertTrue(Validation::decimal(.0));
+ $this->assertTrue(Validation::decimal(.00));
+ $this->assertTrue(Validation::decimal(.01));
+
$this->assertFalse(Validation::decimal('string'));
- $this->assertFalse(Validation::decimal('1234'));
- $this->assertFalse(Validation::decimal('-1234'));
- $this->assertFalse(Validation::decimal('+1234'));
}
/**
View
4 lib/Cake/Utility/Validation.php
@@ -382,9 +382,9 @@ public static function boolean($check) {
public static function decimal($check, $places = null, $regex = null) {
if (is_null($regex)) {
if (is_null($places)) {
- $regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
+ $regex = '/^[-+]?[0-9]*(\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?)?$/';
} else {
- $regex = '/^[-+]?[0-9]*\\.{1}[0-9]{' . $places . '}$/';
+ $regex = '/^[-+]?[0-9]*(\\.{1}[0-9]{' . $places . '})?$/';
}
}
return self::_check($check, $regex);
Something went wrong with that request. Please try again.