Skip to content

Commit a3267a3

Browse files
zll600Leigang Zhang
andauthored
Allow strict integer validation (laravel#56503)
Co-authored-by: Leigang Zhang <leigang.zhang@rightcapital.com>
1 parent fc6e0c6 commit a3267a3

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,10 +1554,15 @@ public function validateInArrayKeys($attribute, $value, $parameters)
15541554
*
15551555
* @param string $attribute
15561556
* @param mixed $value
1557+
* @param array{0: 'strict'} $parameters
15571558
* @return bool
15581559
*/
1559-
public function validateInteger($attribute, $value)
1560+
public function validateInteger($attribute, $value, array $parameters)
15601561
{
1562+
if (($parameters[0] ?? null) === 'strict') {
1563+
return is_int($value);
1564+
}
1565+
15611566
return filter_var($value, FILTER_VALIDATE_INT) !== false;
15621567
}
15631568

tests/Validation/ValidationValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,28 @@ public function testValidateInteger()
32833283
$this->assertTrue($v->passes());
32843284
}
32853285

3286+
public function testValidateIntegerStrict()
3287+
{
3288+
$trans = $this->getIlluminateArrayTranslator();
3289+
$v = new Validator($trans, ['foo' => 'asdad'], ['foo' => 'Integer:strict']);
3290+
$this->assertFalse($v->passes());
3291+
3292+
$v = new Validator($trans, ['foo' => '1.23'], ['foo' => 'Integer:strict']);
3293+
$this->assertFalse($v->passes());
3294+
3295+
$v = new Validator($trans, ['foo' => '-1'], ['foo' => 'Integer:strict']);
3296+
$this->assertFalse($v->passes());
3297+
3298+
$v = new Validator($trans, ['foo' => '1'], ['foo' => 'Integer:strict']);
3299+
$this->assertFalse($v->passes());
3300+
3301+
$v = new Validator($trans, ['foo' => 0.1], ['foo' => 'Integer:strict']);
3302+
$this->assertFalse($v->passes());
3303+
3304+
$v = new Validator($trans, ['foo' => 1], ['foo' => 'Integer:strict']);
3305+
$this->assertTrue($v->passes());
3306+
}
3307+
32863308
public function testValidateDecimal()
32873309
{
32883310
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)