fix(rule): membuat custom rule untuk validasi max integer dan big integer
This commit is contained in:
parent
0f7d1a131d
commit
59d7e8da2c
22
app/Rules/UnsignedBigInteger.php
Normal file
22
app/Rules/UnsignedBigInteger.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class UnsignedBigInteger implements ValidationRule
|
||||
{
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! ctype_digit((string) $value)) {
|
||||
$fail("{$attribute} harus berupa bilangan bulat positif.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (bccomp($value, '0') < 0 || bccomp($value, '18446744073709551615') > 0) {
|
||||
$fail("{$attribute} harus berada di antara 0 dan 18446744073709551615.");
|
||||
}
|
||||
}
|
||||
}
|
||||
22
app/Rules/UnsignedInteger.php
Normal file
22
app/Rules/UnsignedInteger.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
|
||||
class UnsignedInteger implements ValidationRule
|
||||
{
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! ctype_digit((string) $value)) {
|
||||
$fail("{$attribute} harus berupa bilangan bulat positif.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ((int) $value < 0 || (int) $value > 4294967295) {
|
||||
$fail("{$attribute} harus berada di antara 0 dan 4294967295.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user