Skip to content

Commit 4d896fa

Browse files
committed
Added validator field name array support, changed checkout validator rules
1 parent e87f71e commit 4d896fa

File tree

4 files changed

+89
-21
lines changed

4 files changed

+89
-21
lines changed

app/validate/card.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Vvveb
5+
*
6+
* Copyright (C) 2022 Ziadin Givan
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
return [
24+
'card.number' => [
25+
'notEmpty' => '',
26+
'maxLength' => 19,
27+
],
28+
'card.expiryMonth' => [
29+
'notEmpty' => '',
30+
'maxLength' => 2,
31+
],
32+
'card.expiryYear' => [
33+
'notEmpty' => '',
34+
'maxLength' => 2,
35+
],
36+
'card.cvv' => [
37+
'notEmpty' => '',
38+
'maxLength' => 4,
39+
],
40+
];

app/validate/checkout_billing.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,66 +23,66 @@
2323
use function Vvveb\__;
2424

2525
return [
26-
'billing_first_name' => [
26+
'billing_address.first_name' => [
2727
['notEmpty' => '',
2828
'message' => '%s must not be empty', ],
2929

3030
['maxLength' => 100,
3131
'message' => __('%s must not be greater than 100'), ],
3232
],
33-
'billing_last_name' => [
33+
'billing_address.last_name' => [
3434
['notEmpty' => '',
3535
'message' => '%s must not be empty', ],
3636

3737
['maxLength' => 100,
3838
'message' => __('%s must not be greater than 100'), ],
3939
],
4040
/*
41-
'billing_company' => [
41+
'billing_address.company' => [
4242
['notEmpty' => '',
4343
'message' => '%s must not be empty', ],
4444
4545
['maxLength' => 100,
4646
'message' => __('%s must not be greater than 100'), ],
4747
],*/
48-
'billing_address_1' => [
48+
'billing_address.address_1' => [
4949
['notEmpty' => '',
5050
'message' => '%s must not be empty', ],
5151

5252
['maxLength' => 100,
5353
'message' => __('%s must not be greater than 100'), ],
5454
],
55-
'billing_address_2' => [
55+
'billing_address.address_2' => [
5656
['notEmpty' => '',
5757
'message' => '%s must not be empty', ],
5858

5959
['maxLength' => 100,
6060
'message' => __('%s must not be greater than 100'), ],
6161
],
62-
'billing_city' => [
62+
'billing_address.city' => [
6363
['notEmpty' => '',
6464
'message' => '%s must not be empty', ],
6565

6666
['maxLength' => 100,
6767
'message' => __('%s must not be greater than 100'), ],
6868
],
69-
'billing_post_code' => [
69+
'billing_address.post_code' => [
7070
['notEmpty' => '',
7171
'message' => '%s must not be empty', ],
7272

7373
['maxLength' => 100,
7474
'message' => __('%s must not be greater than 100'), ],
7575
],
76-
'billing_country_id' => [
76+
'billing_address.country_id' => [
7777
['notEmpty' => '',
7878
'message' => '%s must not be empty', ],
7979
],
80-
'billing_region_id' => [
80+
'billing_address.region_id' => [
8181
['notEmpty' => '',
8282
'message' => '%s must not be empty', ],
8383
],
8484
/*
85-
'billing_phone_number' => [
85+
'billing_address.phone_number' => [
8686
['notEmpty' => '',
8787
'message' => '%s must not be empty', ],
8888
],*/

app/validate/checkout_shipping.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,60 @@
2323
use function Vvveb\__;
2424

2525
return [
26-
'shipping_first_name' => [
26+
'shipping_address.first_name' => [
2727
['notEmpty' => '',
2828
'message' => '%s must not be empty', ],
2929

3030
['maxLength' => 100,
3131
'message' => __('%s must not be greater than 100'), ],
3232
],
33-
'shipping_last_name' => [
33+
'shipping_address.last_name' => [
3434
['notEmpty' => '',
3535
'message' => '%s must not be empty', ],
3636

3737
['maxLength' => 100,
3838
'message' => __('%s must not be greater than 100'), ],
3939
], /*
40-
'shipping_company' => [
40+
'shipping_address.company' => [
4141
['notEmpty' => '',
4242
'message' => '%s must not be empty', ],
4343
4444
['maxLength' => 100,
4545
'message' => __('%s must not be greater than 100'), ],
4646
],*/
47-
'shipping_address_1' => [
47+
'shipping_address.address_1' => [
4848
['notEmpty' => '',
4949
'message' => '%s must not be empty', ],
5050

5151
['maxLength' => 100,
5252
'message' => __('%s must not be greater than 100'), ],
5353
],
54-
'shipping_address_2' => [
54+
'shipping_address.address_2' => [
5555
['notEmpty' => '',
5656
'message' => '%s must not be empty', ],
5757

5858
['maxLength' => 100,
5959
'message' => __('%s must not be greater than 100'), ],
6060
],
61-
'shipping_city' => [
61+
'shipping_address.city' => [
6262
['notEmpty' => '',
6363
'message' => '%s must not be empty', ],
6464

6565
['maxLength' => 100,
6666
'message' => __('%s must not be greater than 100'), ],
6767
],
68-
'shipping_post_code' => [
68+
'shipping_address.post_code' => [
6969
['notEmpty' => '',
7070
'message' => '%s must not be empty', ],
7171

7272
['maxLength' => 100,
7373
'message' => __('%s must not be greater than 100'), ],
7474
],
75-
'shipping_country_id' => [
75+
'shipping_address.country_id' => [
7676
['notEmpty' => '',
7777
'message' => '%s must not be empty', ],
7878
],
79-
'shipping_region_id' => [
79+
'shipping_address.region_id' => [
8080
['notEmpty' => '',
8181
'message' => '%s must not be empty', ],
8282
],

system/validator.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function __construct($rules) {
4747
'captcha' => __('%s is invalid'),
4848
'session' => __('%s is invalid'),
4949
'email' => __('%s invalid email'),
50+
'integer' => __('%s not numeric'),
5051
'passwordComplexity' => __('%s not complex enough, include uppercase letters and digits'),
5152
];
5253

@@ -70,13 +71,32 @@ function rules($ruleFiles) {
7071
$this->rules = $rules;
7172
}
7273

74+
//remove rule
75+
function removeRule($rule) {
76+
unset($this->rules[$rule]);
77+
}
78+
7379
//remove keys that are not in the validation list
7480
function filter($input) {
7581
$validKeys = array_keys($this->rules);
82+
$filter = [];
83+
84+
foreach ($validKeys as $key) {
85+
if (strpos($key, '.') != false) {
86+
$keys = explode('.', $key);
87+
$filter[$keys[0]][$keys[1]] = arrayPath($input, $key);
88+
} else {
89+
if (isset($input[$key])) {
90+
$filter[$key] = $input[$key];
91+
}
92+
}
93+
}
7694

95+
return $filter;
96+
/*
7797
return array_filter($input, function ($key) use ($validKeys) {
78-
return in_array($key,$validKeys);
79-
}, ARRAY_FILTER_USE_KEY);
98+
return in_array($key, $validKeys);
99+
}, ARRAY_FILTER_USE_KEY);*/
80100
}
81101

82102
function validate($input) {
@@ -234,6 +254,14 @@ function ruleMatch($value, $options, $name, $message, $input) {
234254
return false;
235255
}
236256

257+
function ruleNumeric($value, $options, $name, $message, $input) {
258+
if (! is_numeric($value)) {
259+
return sprintf(__($message), $name, $options);
260+
}
261+
262+
return false;
263+
}
264+
237265
function rulePasswordComplexity($value, $options, $name, $message, $input) {
238266
switch ($options) {
239267
case 'low':

0 commit comments

Comments
 (0)