Initial commit
This commit is contained in:
35
app/Rules/MaxLengthWithoutHtml.php
Normal file
35
app/Rules/MaxLengthWithoutHtml.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user