feat(rules): add rules for validation custom DateFormats
This commit is contained in:
parent
488049d56f
commit
4cef0722cb
28
app/Rules/DateHI.php
Normal file
28
app/Rules/DateHI.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Brick\DateTime\DateTimeException;
|
||||||
|
use Brick\DateTime\LocalTime;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate time in format HH:MM
|
||||||
|
*/
|
||||||
|
class DateHI implements ValidationRule
|
||||||
|
{
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
if (!preg_match('/^\d{2}:\d{2}$/', $value)) {
|
||||||
|
$fail('validation.date_hi');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
LocalTime::parse($value);
|
||||||
|
} catch (DateTimeException $e) {
|
||||||
|
$fail('validation.date_hi');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
app/Rules/DateYMD.php
Normal file
24
app/Rules/DateYMD.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate a date in the format YYYY-MM-DD.
|
||||||
|
*/
|
||||||
|
class DateYMD implements ValidationRule
|
||||||
|
{
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
|
||||||
|
$fail('validation.format.date_ymd');
|
||||||
|
}
|
||||||
|
if (\DateTimeImmutable::createFromFormat("Y-m-d", $value) === false) {
|
||||||
|
$fail('validation.format.date_ymd');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
app/Rules/DateYMDHI.php
Normal file
24
app/Rules/DateYMDHI.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate date in format YYYY-MM-DD HH:MM
|
||||||
|
*/
|
||||||
|
class DateYMDHI implements ValidationRule
|
||||||
|
{
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $value)) {
|
||||||
|
$fail('validation.date_ymdhi');
|
||||||
|
}
|
||||||
|
if (\DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $value . ":00") === false) {
|
||||||
|
$fail('validation.date_ymdhi');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
24
app/Rules/DateYMDHIS.php
Normal file
24
app/Rules/DateYMDHIS.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Rules;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Contracts\Validation\ValidationRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate a date in the format YYYY-MM-DD HH:MM:SS
|
||||||
|
*/
|
||||||
|
class DateYMDHIS implements ValidationRule
|
||||||
|
{
|
||||||
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||||
|
{
|
||||||
|
if (!preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', $value)) {
|
||||||
|
$fail('validation.date_ymdhis');
|
||||||
|
}
|
||||||
|
if (\DateTimeImmutable::createFromFormat("Y-m-d H:i:s", $value) === false) {
|
||||||
|
$fail('validation.date_ymdhis');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user