2024-09-20 08:30:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
|
|
|
|
class IsValidRegex implements ValidationRule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Run the validation rule.
|
|
|
|
*/
|
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail) : void
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
preg_match('/' . $value . '/', '');
|
2024-09-26 23:50:01 +02:00
|
|
|
|
2024-09-20 08:30:39 +02:00
|
|
|
if (preg_last_error() !== PREG_NO_ERROR) {
|
|
|
|
$fail('validation.IsValidRegex')->translate();
|
|
|
|
}
|
2024-09-26 23:50:01 +02:00
|
|
|
} catch (\Throwable $ex) {
|
2024-09-20 08:30:39 +02:00
|
|
|
$fail('validation.IsValidRegex')->translate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|