Skip to content

Commit 3e26749

Browse files
committed
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.
1 parent 658827c commit 3e26749

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Version 1.0 - June 1, 2010
22

3+
- fixed validates_numericality_of to not ignore other options when only_integer is present and matches
34
- fixed set methods on DateTime objects to properly flag attributes as dirty
45
- fixed DateTime attributes to be serialized as strings
56
- support for Oracle
@@ -13,4 +14,4 @@ Version 1.0 - June 1, 2010
1314
- added validates_uniqueness_of
1415
- added dynamic count_by
1516
- added eager loading
16-
- and many bug fixes
17+
- and many bug fixes

lib/Validations.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,16 @@ public function validates_numericality_of($attrs)
288288

289289
if (true === $options['only_integer'] && !is_integer($var))
290290
{
291-
if (preg_match('/\A[+-]?\d+\Z/', (string)($var)))
292-
break;
293-
294-
if (isset($options['message']))
295-
$message = $options['message'];
296-
else
297-
$message = Errors::$DEFAULT_ERROR_MESSAGES['not_a_number'];
291+
if (!preg_match('/\A[+-]?\d+\Z/', (string)($var)))
292+
{
293+
if (isset($options['message']))
294+
$message = $options['message'];
295+
else
296+
$message = Errors::$DEFAULT_ERROR_MESSAGES['not_a_number'];
298297

299-
$this->record->add($attribute, $message);
300-
continue;
298+
$this->record->add($attribute, $message);
299+
continue;
300+
}
301301
}
302302
else
303303
{

test/ValidatesNumericalityOfTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public function test_only_integer()
102102
$this->assert_invalid(array(1.5, '1.5'));
103103
}
104104

105+
public function test_only_integer_matching_does_not_ignore_other_options()
106+
{
107+
BookNumericality::$validates_numericality_of[0]['only_integer'] = true;
108+
BookNumericality::$validates_numericality_of[0]['greater_than'] = 0;
109+
110+
$this->assert_invalid(array(-1,'-1'));
111+
}
112+
105113
public function test_greater_than()
106114
{
107115
BookNumericality::$validates_numericality_of[0]['greater_than'] = 5;
@@ -145,4 +153,4 @@ public function test_greater_than_less_than_and_even()
145153

146154
array_merge(ValidatesNumericalityOfTest::$INTEGERS, ValidatesNumericalityOfTest::$INTEGER_STRINGS);
147155
array_merge(ValidatesNumericalityOfTest::$FLOATS, ValidatesNumericalityOfTest::$FLOAT_STRINGS);
148-
?>
156+
?>

test/ValidationsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BookValidations extends ActiveRecord\Model
99
static $alias_attribute = array('name_alias' => 'name', 'x' => 'secondary_author_id');
1010
static $validates_presence_of = array(array('name'));
1111
static $validates_uniqueness_of = array();
12-
};
12+
}
1313

1414
class ValidationsTest extends DatabaseTest
1515
{
@@ -115,4 +115,4 @@ public function test_get_validation_rules()
115115
$this->assert_true(in_array(array('validator' => 'validates_presence_of'),$validators['name']));
116116
}
117117
};
118-
?>
118+
?>

0 commit comments

Comments
 (0)