Treat float numbers with trailing zeroes removed by PHP, accordingly.

Revert #2800 (28bd6880df) regex changes. Also add more tests.

Signed-off-by: mark_story <mark@mark-story.com>
This commit is contained in:
Ber Clausen 2012-08-24 12:08:21 -03:00 committed by mark_story
parent f06ce1324d
commit b75a6b440a
2 changed files with 12 additions and 5 deletions

View file

@ -1498,16 +1498,20 @@ class ValidationTest extends CakeTestCase {
$this->assertTrue(Validation::decimal('+0123.45e6')); $this->assertTrue(Validation::decimal('+0123.45e6'));
$this->assertTrue(Validation::decimal('-0123.45e6')); $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.56));
$this->assertTrue(Validation::decimal(1234.00)); $this->assertTrue(Validation::decimal(1234.00));
$this->assertTrue(Validation::decimal('1234.00'));
$this->assertTrue(Validation::decimal(.0)); $this->assertTrue(Validation::decimal(.0));
$this->assertTrue(Validation::decimal(.00)); $this->assertTrue(Validation::decimal(.00));
$this->assertTrue(Validation::decimal('.00'));
$this->assertTrue(Validation::decimal(.01)); $this->assertTrue(Validation::decimal(.01));
$this->assertTrue(Validation::decimal('.01'));
$this->assertFalse(Validation::decimal(''));
$this->assertFalse(Validation::decimal('string')); $this->assertFalse(Validation::decimal('string'));
$this->assertFalse(Validation::decimal('1234'));
$this->assertFalse(Validation::decimal('-1234'));
$this->assertFalse(Validation::decimal('+1234'));
} }
/** /**

View file

@ -381,11 +381,14 @@ class Validation {
* @return boolean Success * @return boolean Success
*/ */
public static function decimal($check, $places = null, $regex = null) { public static function decimal($check, $places = null, $regex = null) {
if (is_float($check) && floor($check) === $check) {
$check = sprintf("%.1f", $check);
}
if (is_null($regex)) { if (is_null($regex)) {
if (is_null($places)) { if (is_null($places)) {
$regex = '/^[-+]?[0-9]*(\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?)?$/'; $regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
} else { } else {
$regex = '/^[-+]?[0-9]*(\\.{1}[0-9]{' . $places . '})?$/'; $regex = '/^[-+]?[0-9]*\\.{1}[0-9]{' . $places . '}$/';
} }
} }
return self::_check($check, $regex); return self::_check($check, $regex);