Permalink
Browse files

fixed validates_numericality_of to not ignore other options when only…

…_integer is present and matches

example of bug:

static $validates_numericality_of = array(
   array('quantity', 'greater_than' => 0, 'only_integer' => true)
);

Any values less than 0 would pass the validator when it should fail.
  • Loading branch information...
kla committed May 29, 2010
1 parent 658827c commit 3e267498ba98d5c7b51f40d11b7f5976a404add2
Showing with 22 additions and 13 deletions.
  1. +2 −1 CHANGELOG
  2. +9 −9 lib/Validations.php
  3. +9 −1 test/ValidatesNumericalityOfTest.php
  4. +2 −2 test/ValidationsTest.php
View
@@ -1,5 +1,6 @@
Version 1.0 - June 1, 2010
- fixed validates_numericality_of to not ignore other options when only_integer is present and matches
- fixed set methods on DateTime objects to properly flag attributes as dirty
- fixed DateTime attributes to be serialized as strings
- support for Oracle
@@ -13,4 +14,4 @@ Version 1.0 - June 1, 2010
- added validates_uniqueness_of
- added dynamic count_by
- added eager loading
- and many bug fixes
- and many bug fixes
View
@@ -288,16 +288,16 @@ public function validates_numericality_of($attrs)
if (true === $options['only_integer'] && !is_integer($var))
{
if (preg_match('/\A[+-]?\d+\Z/', (string)($var)))
break;
if (isset($options['message']))
$message = $options['message'];
else
$message = Errors::$DEFAULT_ERROR_MESSAGES['not_a_number'];
if (!preg_match('/\A[+-]?\d+\Z/', (string)($var)))
{
if (isset($options['message']))
$message = $options['message'];
else
$message = Errors::$DEFAULT_ERROR_MESSAGES['not_a_number'];
$this->record->add($attribute, $message);
continue;
$this->record->add($attribute, $message);
continue;
}
}
else
{
@@ -102,6 +102,14 @@ public function test_only_integer()
$this->assert_invalid(array(1.5, '1.5'));
}
public function test_only_integer_matching_does_not_ignore_other_options()
{
BookNumericality::$validates_numericality_of[0]['only_integer'] = true;
BookNumericality::$validates_numericality_of[0]['greater_than'] = 0;
$this->assert_invalid(array(-1,'-1'));
}
public function test_greater_than()
{
BookNumericality::$validates_numericality_of[0]['greater_than'] = 5;
@@ -145,4 +153,4 @@ public function test_greater_than_less_than_and_even()
array_merge(ValidatesNumericalityOfTest::$INTEGERS, ValidatesNumericalityOfTest::$INTEGER_STRINGS);
array_merge(ValidatesNumericalityOfTest::$FLOATS, ValidatesNumericalityOfTest::$FLOAT_STRINGS);
?>
?>
View
@@ -9,7 +9,7 @@ class BookValidations extends ActiveRecord\Model
static $alias_attribute = array('name_alias' => 'name', 'x' => 'secondary_author_id');
static $validates_presence_of = array(array('name'));
static $validates_uniqueness_of = array();
};
}
class ValidationsTest extends DatabaseTest
{
@@ -115,4 +115,4 @@ public function test_get_validation_rules()
$this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name']));
}
};
?>
?>

0 comments on commit 3e26749

Please sign in to comment.