Skip to content

Commit

Permalink
fixed validates_numericality_of to not ignore other options when only…
Browse files Browse the repository at this point in the history
…_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 3e26749
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
@@ -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
Expand All @@ -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
18 changes: 9 additions & 9 deletions lib/Validations.php
Expand Up @@ -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
{
Expand Down
10 changes: 9 additions & 1 deletion test/ValidatesNumericalityOfTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
?>
?>
4 changes: 2 additions & 2 deletions test/ValidationsTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -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.