File tree Expand file tree Collapse file tree 5 files changed +161
-0
lines changed Expand file tree Collapse file tree 5 files changed +161
-0
lines changed Original file line number Diff line number Diff line change @@ -54,6 +54,7 @@ public function rules()
5454- [ ` Float Number ` ] ( #floatnumber )
5555- [ ` Hash ` ] ( #hash )
5656- [ ` Image URL ` ] ( #imageurl )
57+ - [ ` Name ` ] ( #name )
5758- [ ` Phone ` ] ( #phone )
5859- [ ` Username ` ] ( #username )
5960- [ ` Without Spaces ` ] ( #withoutspaces )
@@ -267,6 +268,27 @@ public function rules()
267268}
268269```
269270
271+ ### ` Name `
272+
273+ The field under validation must be a valid name.
274+
275+ - no emoji
276+ - no number (if $allowNumber flag is true, it will accept numbers, default is false)
277+ - special characters are allowed
278+
279+ public Arifszn\AdvancedValidation\Rules\Name::__ construct(bool $allowNumber = false, string $errorMessage = null)
280+
281+ ``` php
282+ use Arifszn\AdvancedValidation\Rules\Name;
283+
284+ public function rules()
285+ {
286+ return [
287+ 'name' => [new Name()],
288+ ];
289+ }
290+ ```
291+
270292### ` Phone `
271293
272294The field under validation must be a valid phone number.
Original file line number Diff line number Diff line change 1313 'float_number ' => 'The :attribute must be a float number. ' ,
1414 'hash ' => 'The :attribute must be a hash of :algorithm algorithm. ' ,
1515 'image_url ' => 'The :attribute must be a valid image URL. ' ,
16+ 'name ' => 'The :attribute must be a valid name. ' ,
1617 'phone ' => 'The :attribute must be a valid phone number. ' ,
1718 'username ' => 'The :attribute must be a valid username. ' ,
1819 'without_spaces ' => 'The :attribute must not contain spaces. ' ,
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Arifszn \AdvancedValidation \Rules ;
4+
5+ use Illuminate \Contracts \Validation \Rule ;
6+
7+ /**
8+ * The field under validation must be a valid name.
9+ *
10+ * - no emoji
11+ * - no number (if $allowNumber flag is false)
12+ * - special characters are allowed
13+ *
14+ * @package Arifszn\AdvancedValidation\Rules
15+ */
16+ class Name implements Rule
17+ {
18+ /**
19+ * @var bool
20+ */
21+ private $ allowNumber ;
22+
23+ /**
24+ * @var string
25+ */
26+ private $ errorMessage ;
27+
28+ /**
29+ * Create a new rule instance.
30+ *
31+ * @param bool $allowNumber Allow number.
32+ * @param string|null $errorMessage Custom error message.
33+ * @return void
34+ */
35+ public function __construct (bool $ allowNumber = false , string $ errorMessage = null )
36+ {
37+ $ this ->allowNumber = $ allowNumber ;
38+ $ this ->errorMessage = $ errorMessage ;
39+ }
40+
41+ /**
42+ * Determine if the validation rule passes.
43+ *
44+ * @param string $attribute
45+ * @param mixed $value
46+ * @return bool
47+ */
48+ public function passes ($ attribute , $ value )
49+ {
50+ // check empty
51+ if (!trim ($ value )) {
52+ return false ;
53+ }
54+
55+ // check no emoji
56+ if (preg_match ('/\p{S}/u ' , $ value )) {
57+ return false ;
58+ }
59+
60+ if (!$ this ->allowNumber && !preg_match ('/^([^0-9]*)$/ ' , $ value )) {
61+ return false ;
62+ }
63+
64+ return true ;
65+ }
66+
67+ /**
68+ * Get the validation error message.
69+ *
70+ * @return string
71+ */
72+ public function message ()
73+ {
74+ return $ this ->errorMessage ? $ this ->errorMessage : trans ('advancedValidation::validation.name ' );
75+ }
76+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Arifszn \AdvancedValidation \Tests \Rules ;
4+
5+ use Arifszn \AdvancedValidation \Rules \Name ;
6+ use Arifszn \AdvancedValidation \Tests \TestCase ;
7+
8+ class NameTest extends TestCase
9+ {
10+ /**
11+ * @dataProvider providerWithoutAllowNumber
12+ */
13+ public function testValidationWithoutAllowNumber ($ result , $ value )
14+ {
15+ $ this ->assertEquals ($ result , (new Name (false ))->passes ('foo ' , $ value ));
16+ }
17+
18+ /**
19+ * @dataProvider providerWithAllowNumber
20+ */
21+ public function testValidationWithAllowNumber ($ result , $ value )
22+ {
23+ $ this ->assertEquals ($ result , (new Name (true ))->passes ('foo ' , $ value ));
24+ }
25+
26+ public function providerWithoutAllowNumber ()
27+ {
28+ return array_merge ($ this ->shared (), [
29+ [false , '1234 abc DEF ' ],
30+ [false , '1234abcDEF ' ],
31+ [false , '消极的123 ' ],
32+ ]);
33+ }
34+
35+ public function providerWithAllowNumber ()
36+ {
37+ return array_merge ($ this ->shared (), [
38+ [true , '1234 abc DEF ' ],
39+ [true , '1234abcDEF ' ],
40+ [true , '消极的123 ' ],
41+ ]);
42+ }
43+
44+ private function shared ()
45+ {
46+ return [
47+ [true , 'foobar ' ],
48+ [true , 'foobar ' ],
49+ [true , 'a ' ],
50+ [true , '消极的 ' ],
51+ [true , '### ' ],
52+ [true , '** ' ],
53+ [false , '😀 ' ],
54+ [false , 'john 😀 ' ],
55+ [false , '' ],
56+ [false , ' ' ],
57+ ];
58+ }
59+ }
Original file line number Diff line number Diff line change @@ -43,6 +43,9 @@ public function provider()
4343 [false , 'a b c ' ],
4444 [false , 'foobar ' ],
4545 [false , '😀 ' ],
46+ [false , 'john😀 ' ],
47+ [false , '' ],
48+ [false , ' ' ],
4649 ];
4750 }
4851}
You can’t perform that action at this time.
0 commit comments