2023-10-17 09:09:45 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Rules;
|
|
|
|
|
|
2024-06-10 16:10:14 +02:00
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
2023-10-17 09:09:45 +02:00
|
|
|
|
2024-06-10 16:10:14 +02:00
|
|
|
class ValidUrl implements ValidationRule
|
2023-10-17 09:09:45 +02:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the validation rule passes.
|
|
|
|
|
*
|
|
|
|
|
* @param string $attribute
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function passes($attribute, $value)
|
|
|
|
|
{
|
|
|
|
|
// Define the regular expression to match the desired URL patterns
|
|
|
|
|
$pattern = '/^(https?:\/\/)?(www\.)?[\w.-]+\.\w+(:\d+)?(\/[^\s]*)?$/';
|
|
|
|
|
|
|
|
|
|
return preg_match($pattern, $value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-10 16:10:14 +02:00
|
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
|
|
|
{
|
|
|
|
|
if (!$this->passes($attribute, $value)) {
|
|
|
|
|
$fail($this->message());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 09:09:45 +02:00
|
|
|
/**
|
|
|
|
|
* Get the validation error message.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function message()
|
|
|
|
|
{
|
|
|
|
|
return 'The :attribute format is invalid.';
|
|
|
|
|
}
|
|
|
|
|
}
|