Skip to content

Commit 66975f6

Browse files
committed
[11.x] Fix validation to not throw incompatible validation exception
fixes #55944 Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 0a5f42b commit 66975f6

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,12 @@ public function validateBetween($attribute, $value, $parameters)
471471
{
472472
$this->requireParameterCount(2, $parameters, 'between');
473473

474-
return with(
475-
BigNumber::of($this->getSize($attribute, $value)),
476-
fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]))
477-
);
474+
return rescue(function () use ($attribute, $value, $parameters) {
475+
return with(
476+
BigNumber::of($this->getSize($attribute, $value)),
477+
fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]))
478+
);
479+
}, false, false);
478480
}
479481

480482
/**
@@ -1223,22 +1225,29 @@ public function validateGt($attribute, $value, $parameters)
12231225
$this->shouldBeNumeric($attribute, 'Gt');
12241226

12251227
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1226-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($this->trim($parameters[0]));
1228+
return rescue(
1229+
fn () => BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($this->trim($parameters[0])), false, false
1230+
);
12271231
}
12281232

12291233
if (is_numeric($parameters[0])) {
12301234
return false;
12311235
}
12321236

12331237
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1234-
return BigNumber::of($this->trim($value))->isGreaterThan($this->trim($comparedToValue));
1238+
return rescue(
1239+
fn () => BigNumber::of($this->trim($value))->isGreaterThan($this->trim($comparedToValue)),
1240+
false
1241+
);
12351242
}
12361243

12371244
if (! $this->isSameType($value, $comparedToValue)) {
12381245
return false;
12391246
}
12401247

1241-
return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue);
1248+
return rescue(
1249+
fn () => $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue), false, false
1250+
);
12421251
}
12431252

12441253
/**
@@ -1258,7 +1267,9 @@ public function validateLt($attribute, $value, $parameters)
12581267
$this->shouldBeNumeric($attribute, 'Lt');
12591268

12601269
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1261-
return BigNumber::of($this->getSize($attribute, $value))->isLessThan($this->trim($parameters[0]));
1270+
return rescue(
1271+
fn () => BigNumber::of($this->getSize($attribute, $value))->isLessThan($this->trim($parameters[0])), false, false
1272+
);
12621273
}
12631274

12641275
if (is_numeric($parameters[0])) {
@@ -1273,7 +1284,9 @@ public function validateLt($attribute, $value, $parameters)
12731284
return false;
12741285
}
12751286

1276-
return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue);
1287+
return rescue(
1288+
fn () => $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue), false, false
1289+
);
12771290
}
12781291

12791292
/**
@@ -1293,22 +1306,29 @@ public function validateGte($attribute, $value, $parameters)
12931306
$this->shouldBeNumeric($attribute, 'Gte');
12941307

12951308
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1296-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1309+
return rescue(
1310+
fn () => BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0])), false, false
1311+
);
12971312
}
12981313

12991314
if (is_numeric($parameters[0])) {
13001315
return false;
13011316
}
13021317

13031318
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1304-
return BigNumber::of($this->trim($value))->isGreaterThanOrEqualTo($this->trim($comparedToValue));
1319+
return rescue(
1320+
fn () => BigNumber::of($this->trim($value))->isGreaterThanOrEqualTo($this->trim($comparedToValue)),
1321+
false
1322+
);
13051323
}
13061324

13071325
if (! $this->isSameType($value, $comparedToValue)) {
13081326
return false;
13091327
}
13101328

1311-
return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue);
1329+
return rescue(
1330+
fn () => $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue), false, false
1331+
);
13121332
}
13131333

13141334
/**
@@ -1328,7 +1348,9 @@ public function validateLte($attribute, $value, $parameters)
13281348
$this->shouldBeNumeric($attribute, 'Lte');
13291349

13301350
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1331-
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1351+
return rescue(
1352+
fn () => BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0])), false, false
1353+
);
13321354
}
13331355

13341356
if (is_numeric($parameters[0])) {
@@ -1343,7 +1365,9 @@ public function validateLte($attribute, $value, $parameters)
13431365
return false;
13441366
}
13451367

1346-
return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue);
1368+
return rescue(
1369+
fn () => $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue), false, false
1370+
);
13471371
}
13481372

13491373
/**
@@ -1544,7 +1568,9 @@ public function validateMax($attribute, $value, $parameters)
15441568
return false;
15451569
}
15461570

1547-
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1571+
return rescue(
1572+
fn () => BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0])), false, false
1573+
);
15481574
}
15491575

15501576
/**
@@ -1646,7 +1672,9 @@ public function validateMin($attribute, $value, $parameters)
16461672
{
16471673
$this->requireParameterCount(1, $parameters, 'min');
16481674

1649-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1675+
return rescue(
1676+
fn () => BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0])), false, false
1677+
);
16501678
}
16511679

16521680
/**
@@ -2466,7 +2494,9 @@ public function validateSize($attribute, $value, $parameters)
24662494
{
24672495
$this->requireParameterCount(1, $parameters, 'size');
24682496

2469-
return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0]));
2497+
return rescue(
2498+
fn () => BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0])), false, false
2499+
);
24702500
}
24712501

24722502
/**
@@ -2738,6 +2768,8 @@ protected function trim($value)
27382768
* @param string $attribute
27392769
* @param mixed $value
27402770
* @return mixed
2771+
*
2772+
* @throws \Illuminate\Support\Exceptions\MathException
27412773
*/
27422774
protected function ensureExponentWithinAllowedRange($attribute, $value)
27432775
{

tests/Validation/ValidationValidatorTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Illuminate\Database\Eloquent\Model;
1919
use Illuminate\Support\Arr;
2020
use Illuminate\Support\Carbon;
21-
use Illuminate\Support\Exceptions\MathException;
2221
use Illuminate\Support\Stringable;
2322
use Illuminate\Translation\ArrayLoader;
2423
use Illuminate\Translation\Translator;
@@ -9534,10 +9533,7 @@ public function testItLimitsLengthOfScientificNotationExponent($value)
95349533
$trans = $this->getIlluminateArrayTranslator();
95359534
$validator = new Validator($trans, ['foo' => $value], ['foo' => 'numeric|min:3']);
95369535

9537-
$this->expectException(MathException::class);
9538-
$this->expectExceptionMessage('Scientific notation exponent outside of allowed range.');
9539-
9540-
$validator->passes();
9536+
$this->assertFalse($validator->passes());
95419537
}
95429538

95439539
public static function outsideRangeExponents()
@@ -9592,10 +9588,8 @@ public function testItCanConfigureAllowedExponentRange()
95929588
$this->assertSame('1.0e-1000', $value);
95939589

95949590
$withinRange = false;
9595-
$this->expectException(MathException::class);
9596-
$this->expectExceptionMessage('Scientific notation exponent outside of allowed range.');
95979591

9598-
$validator->passes();
9592+
$this->assertFalse($validator->passes());
95999593
}
96009594

96019595
protected function getTranslator()

0 commit comments

Comments
 (0)