Skip to content

Commit

Permalink
Revert "Merge pull request #606 from ManukMinasyan/fix/rule-no-blank-…
Browse files Browse the repository at this point in the history
…characters"

This reverts commit 0b044cc, reversing
changes made to 2fc192b.
  • Loading branch information
nunomaduro committed Sep 18, 2024
1 parent a1b7152 commit 39343c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
12 changes: 3 additions & 9 deletions app/Rules/NoBlankCharacters.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,17 @@

final readonly class NoBlankCharacters implements ValidationRule
{
private const string BLANK_CHARACTERS_PATTERN = '/^[\s\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{2028}\x{205F}\x{3000}]*$/u';

private const string FORMAT_CHARACTERS_PATTERN = '/\p{Cf}/u';

/**
* Validate the value of the given attribute.
* Run the validation rule.
*
* @param string $attribute The name of the attribute being validated
* @param mixed $value The value of the attribute
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = type($value)->asString();

if (preg_match(self::BLANK_CHARACTERS_PATTERN, $value) || preg_match(self::FORMAT_CHARACTERS_PATTERN, $value)) {
$fail('The :attribute field cannot be empty or contain only blank characters.');
if (preg_match("/\p{Cf}/u", $value)) {
$fail('The :attribute field cannot contain blank characters.');
}
}
}
23 changes: 4 additions & 19 deletions tests/Unit/Rules/NoBlankCharactersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

use App\Rules\NoBlankCharacters;

test('validation fails for strings containing blank characters', function (string $name) {
test('with blank characters', function (string $name) {
$rule = new NoBlankCharacters;

$fail = function (string $errorMessage) {
// Capture the error message
$this->errorMessage = $errorMessage;
};
$fail = fn (string $errorMessage) => $this->fail($errorMessage);

// Validate the input
$rule->validate('name', $name, $fail);

expect(isset($this->errorMessage))->toBeTrue();
expect(true)->toBeFalse();
})->with([
"\u{200E}",
"\u{200E}\u{200E}",
Expand All @@ -26,18 +22,7 @@
"测试\u{200E}",
"ⓣⓔⓢⓣ\u{200E}",
' ',
"\u{2005}",
"\u{2006}",
"\u{2007}",
"\u{2008}",
"\u{2009}",
"\u{200A}",
"\u{2028}",
"\u{205F}",
"\u{3000}",
" \u{2005} ",
"\u{2007}\u{2008}\u{2009}",
]);
])->fails();

test('without blank characters', function (string $name) {
$rule = new NoBlankCharacters;
Expand Down

0 comments on commit 39343c0

Please sign in to comment.