Skip to content

Commit 417e873

Browse files
strict uppercase/lowercase rule
1 parent 7933938 commit 417e873

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

src/Constants/ValidationRegex.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ValidationRegex
1212
/**
1313
* Only letters
1414
*/
15-
public const LETTERS = 'regex:/\pL/u';
15+
public const LETTERS_ONLY = '/^\p{L}+$/u';
1616

1717
/**
1818
* Letters and spaces

src/Rules/LowercaseWord.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Validation\ValidationRule;
77
use Illuminate\Support\Str;
8+
use Shergela\Validations\Constants\ValidationRegex;
89

910
class LowercaseWord implements ValidationRule
1011
{
@@ -28,6 +29,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2829

2930
$count = count($pregSplit);
3031

32+
if (! preg_match(ValidationRegex::LETTERS_ONLY, $toString)) {
33+
$fail("The :attribute ($toString) must be a alphabetic word.");
34+
return;
35+
}
36+
3137
if ($count > 1) {
3238
$fail("The :attribute ($toString) must be one lowercase word. ($count) words given.");
3339
return;

src/Rules/UppercaseWord.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Contracts\Validation\ValidationRule;
77
use Illuminate\Support\Str;
8+
use Shergela\Validations\Constants\ValidationRegex;
89

910
class UppercaseWord implements ValidationRule
1011
{
@@ -28,6 +29,11 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
2829

2930
$count = count($pregSplit);
3031

32+
if (! preg_match(ValidationRegex::LETTERS_ONLY, $toString)) {
33+
$fail("The :attribute ($toString) must be a alphabetic word.");
34+
return;
35+
}
36+
3137
if ($count > 1) {
3238
$fail("The :attribute ($toString) must be one uppercase word. ($count) words given.");
3339
return;

0 commit comments

Comments
 (0)