File tree Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Expand file tree Collapse file tree 4 files changed +120
-0
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ public function rules()
4949- [ ` Btc Address ` ] ( #btcaddress )
5050- [ ` Credit Card ` ] ( #creditcard )
5151- [ ` Data URI ` ] ( #datauri )
52+ - [ ` Float Number ` ] ( #floatnumber )
5253- [ ` Image URL ` ] ( #imageurl )
5354- [ ` Phone ` ] ( #phone )
5455- [ ` Without Spaces ` ] ( #withoutspaces )
@@ -159,6 +160,21 @@ public function rules()
159160}
160161```
161162
163+ ### ` FloatNumber `
164+
165+ The field under validation must be a float number.
166+
167+ ``` php
168+ use Arifszn\AdvancedValidation\Rules\FloatNumber;
169+
170+ public function rules()
171+ {
172+ return [
173+ 'foo' => [new FloatNumber()],
174+ ];
175+ }
176+ ```
177+
162178### ` ImageURL `
163179
164180The field under validation must be a valid image URL.
Original file line number Diff line number Diff line change 99 'btc_address ' => 'The :attribute must be a valid BTC address. ' ,
1010 'credit_card ' => 'The :attribute must be a valid credit card number. ' ,
1111 'data_uri ' => 'The :attribute must have data uri format. ' ,
12+ 'float_number ' => 'The :attribute must be a float number ' ,
1213 'image_url ' => 'The :attribute must be a valid image URL. ' ,
1314 'phone ' => 'The :attribute must be a valid phone number. ' ,
1415 '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 float number.
9+ *
10+ * @package Arifszn\AdvancedValidation\Rules
11+ */
12+ class FloatNumber implements Rule
13+ {
14+ /**
15+ * @var string
16+ */
17+ private $ errorMessage ;
18+
19+ /**
20+ * Create a new rule instance.
21+ *
22+ * @param string|null $errorMessage Custom error message.
23+ * @return void
24+ */
25+ public function __construct (string $ errorMessage = null )
26+ {
27+ $ this ->errorMessage = $ errorMessage ;
28+ }
29+
30+ /**
31+ * Determine if the validation rule passes.
32+ *
33+ * @param string $attribute
34+ * @param mixed $value
35+ * @return bool
36+ */
37+ public function passes ($ attribute , $ value )
38+ {
39+ $ regex = '/^(?:[-+])?(?:[0-9]+)?(?: \\.[0-9]*)?(?:[eE][ \\+ \\-]?(?:[0-9]+))?$/ ' ;
40+
41+ if ($ value === '' || $ value === '. ' || $ value === '- ' || $ value === '+ ' ) {
42+ return false ;
43+ }
44+
45+ $ value = str_replace (', ' , '. ' , $ value );
46+
47+ return is_numeric ($ value ) && preg_match ($ regex , $ value );
48+ }
49+
50+ /**
51+ * Get the validation error message.
52+ *
53+ * @return string
54+ */
55+ public function message ()
56+ {
57+ return $ this ->errorMessage ? $ this ->errorMessage : trans ('advancedValidation::validation.float_number ' );
58+ }
59+ }
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 \FloatNumber ;
6+ use Arifszn \AdvancedValidation \Tests \TestCase ;
7+
8+ class FloatNumberTest extends TestCase
9+ {
10+ /**
11+ * @dataProvider provider
12+ */
13+ public function testValidation ($ result , $ value )
14+ {
15+ $ this ->assertEquals ($ result , (new FloatNumber ())->passes ('foo ' , $ value ));
16+ }
17+
18+ public function provider ()
19+ {
20+ return [
21+ [true , '123 ' ],
22+ [true , '123. ' ],
23+ [true , '123.123 ' ],
24+ [true , '-123.123 ' ],
25+ [true , '-0.123 ' ],
26+ [true , '+0.123 ' ],
27+ [true , '0.123 ' ],
28+ [true , '.0 ' ],
29+ [true , '-.123 ' ],
30+ [true , '+.123 ' ],
31+ [true , '01.123 ' ],
32+ [true , '-0.22250738585072011e-307 ' ],
33+
34+ [false , '+ ' ],
35+ [false , '- ' ],
36+ [false , ' ' ],
37+ [false , '' ],
38+ [false , '. ' ],
39+ [false , 'foo ' ],
40+ [false , '20.foo ' ],
41+ [false , '2020-01-06T14:31:00.135Z ' ],
42+ ];
43+ }
44+ }
You can’t perform that action at this time.
0 commit comments