File tree Expand file tree Collapse file tree 6 files changed +121
-0
lines changed Expand file tree Collapse file tree 6 files changed +121
-0
lines changed Original file line number Diff line number Diff line change @@ -284,6 +284,8 @@ Below is list of all available validation rules
284
284
* [ min] ( #rule-min )
285
285
* [ max] ( #rule-max )
286
286
* [ between] ( #rule-between )
287
+ * [ digits] ( #rule-digits )
288
+ * [ digits_between] ( #rule-digits_between )
287
289
* [ url] ( #rule-url )
288
290
* [ ip] ( #rule-ip )
289
291
* [ ipv4] ( #rule-ipv4 )
@@ -477,6 +479,16 @@ Value size calculated in same way like `min` rule.
477
479
The field under this rule must have a size between min and max params.
478
480
Value size calculated in same way like ` min ` and ` max ` rule.
479
481
482
+ <a id =" rule-digits " ></a >
483
+ #### digits: value
484
+
485
+ The field under validation must be numeric and must have an exact length of ` value ` .
486
+
487
+ <a id =" rule-digits_between " ></a >
488
+ #### digits_between: min ,max
489
+
490
+ The field under validation must have a length between the given ` min ` and ` max ` .
491
+
480
492
<a id =" rule-url " ></a >
481
493
#### url
482
494
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Rakit \Validation \Rules ;
4
+
5
+ use Rakit \Validation \Rule ;
6
+
7
+ class Digits extends Rule
8
+ {
9
+
10
+ protected $ message = "The :attribute must be numeric and must have an exact length of :length " ;
11
+
12
+ protected $ fillable_params = ['length ' ];
13
+
14
+ public function check ($ value )
15
+ {
16
+ $ this ->requireParameters ($ this ->fillable_params );
17
+
18
+ $ length = (int ) $ this ->parameter ('length ' );
19
+
20
+ return ! preg_match ('/[^0-9]/ ' , $ value )
21
+ && strlen ((string ) $ value ) == $ length ;
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Rakit \Validation \Rules ;
4
+
5
+ use Rakit \Validation \Rule ;
6
+
7
+ class DigitsBetween extends Rule
8
+ {
9
+
10
+ protected $ message = "The :attribute must have a length between the given :min and :max " ;
11
+
12
+ protected $ fillable_params = ['min ' , 'max ' ];
13
+
14
+ public function check ($ value )
15
+ {
16
+ $ this ->requireParameters ($ this ->fillable_params );
17
+
18
+ $ min = (int ) $ this ->parameter ('min ' );
19
+ $ max = (int ) $ this ->parameter ('max ' );
20
+
21
+ $ length = strlen ((string ) $ value );
22
+
23
+ return ! preg_match ('/[^0-9]/ ' , $ value )
24
+ && $ length >= $ min && $ length <= $ max ;
25
+ }
26
+
27
+ }
Original file line number Diff line number Diff line change @@ -107,6 +107,8 @@ protected function registerBaseValidators()
107
107
'lowercase ' => new Rules \Lowercase ,
108
108
'uppercase ' => new Rules \Uppercase ,
109
109
'json ' => new Rules \Json ,
110
+ 'digits ' => new Rules \Digits ,
111
+ 'digits_between ' => new Rules \DigitsBetween ,
110
112
'defaults ' => new Rules \Defaults ,
111
113
'default ' => new Rules \Defaults , // alias of defaults
112
114
];
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Rakit \Validation \Rules \DigitsBetween ;
4
+
5
+ class DigitsBetweenTest extends PHPUnit_Framework_TestCase
6
+ {
7
+
8
+ public function setUp ()
9
+ {
10
+ $ this ->rule = new DigitsBetween ;
11
+ }
12
+
13
+ public function testValids ()
14
+ {
15
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([2 , 6 ])->check (12345 ));
16
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([2 , 3 ])->check (12 ));
17
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([2 , 3 ])->check (123 ));
18
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([3 , 5 ])->check ('12345 ' ));
19
+ }
20
+
21
+ public function testInvalids ()
22
+ {
23
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([4 , 6 ])->check (12 ));
24
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([1 , 3 ])->check (12345 ));
25
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([1 , 3 ])->check (12345 ));
26
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([3 , 6 ])->check ('foobar ' ));
27
+ }
28
+
29
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ use Rakit \Validation \Rules \Digits ;
4
+
5
+ class DigitsTest extends PHPUnit_Framework_TestCase
6
+ {
7
+
8
+ public function setUp ()
9
+ {
10
+ $ this ->rule = new Digits ;
11
+ }
12
+
13
+ public function testValids ()
14
+ {
15
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([4 ])->check (1243 ));
16
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([6 ])->check (124567 ));
17
+ $ this ->assertTrue ($ this ->rule ->fillParameters ([3 ])->check ('123 ' ));
18
+ }
19
+
20
+ public function testInvalids ()
21
+ {
22
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([7 ])->check (12345678 ));
23
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([4 ])->check (12 ));
24
+ $ this ->assertFalse ($ this ->rule ->fillParameters ([3 ])->check ('foo ' ));
25
+ }
26
+
27
+ }
You can’t perform that action at this time.
0 commit comments