35 lines
859 B
PHP
35 lines
859 B
PHP
<?php
|
|
|
|
namespace App\Rules;
|
|
|
|
use Closure;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
|
|
class MaxLengthWithoutHtml implements ValidationRule
|
|
{
|
|
/**
|
|
* The maximum length allowed for the stripped text.
|
|
*/
|
|
protected int $maxLength;
|
|
|
|
/**
|
|
* Create a new rule instance.
|
|
*/
|
|
public function __construct(int $maxLength)
|
|
{
|
|
$this->maxLength = $maxLength;
|
|
}
|
|
|
|
/**
|
|
* Run the validation rule.
|
|
*/
|
|
public function validate(string $attribute, mixed $value, Closure $fail): void
|
|
{
|
|
$strippedText = strip_tags($value ?? '');
|
|
$currentLength = strlen($strippedText);
|
|
|
|
if ($currentLength > $this->maxLength) {
|
|
$fail("The {$attribute} field must not exceed {$this->maxLength} characters (HTML tags excluded). Currently {$currentLength} characters.");
|
|
}
|
|
}
|
|
} |