Skip to content

Commit 3af358d

Browse files
committed
number and req fields
1 parent 41c26a9 commit 3af358d

File tree

3 files changed

+89
-32
lines changed

3 files changed

+89
-32
lines changed

index.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,43 @@
1818
'max_length' => 'Password is too long',
1919
'min_length' => 'Password is too short'
2020
]
21+
],
22+
'number' => [
23+
'rules' => 'is_numeric|max_length[8]|min_length[3]',
24+
'error_messages' => [
25+
'is_numeric' => ' Sayısal olmalı ...',
26+
'max_length' => 'Number is too long',
27+
'min_length' => 'Number is too short'
28+
]
29+
],
30+
'req' => [
31+
'rules' => 'required',
32+
'error_messages' => [
33+
'required' => 'This field is required'
34+
35+
]
2136
]
2237
];
2338

24-
$_POST = ['username' => '12345', 'password' => '12345'];
39+
$_POST = ['username' => '12345', 'password' => '12345', 'number' => '123sa45'];
2540

2641

2742

2843
$checker = $validation->validateRule($_POST, $rules);
2944

3045

46+
47+
3148
if ($checker){
3249
//no errors
33-
echo "No errors";
50+
foreach ($validation->getErrors() as $error){
51+
echo $error . "<br>";
52+
}
53+
3454
}else{
35-
print_r($validation->getErrors());
55+
56+
57+
echo "No errors";
3658
}
3759

3860

src/config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Config
2121
protected static $rule_pattern_check = [
2222
'/max_length/' => [
2323
'parameter' => true,
24+
"parameter_type" => "numeric", //string or numeric
2425
'name' => 'max_length',
2526
'pattern' => '/max_length\[\d+\]/',
2627
'pattern_check' => '/max_length\[(\d+)\]/',
@@ -29,12 +30,31 @@ class Config
2930
],
3031
'/min_length/' => [
3132
'parameter' => true,
33+
"parameter_type" => "numeric", //string or numeric
3234
'name' => 'min_length',
3335
'pattern' => '/min_length\[\d+\]/',
3436
'pattern_check' => '/min_length\[(\d+)\]/',
3537
'pattern_type' => 'numeric',
3638
'not_numeric_rule' => 'min_length can only be controlled numerically. example:min_length[1]'
3739
],
40+
'/is_numeric/' => [
41+
'parameter' => false,
42+
"parameter_type" => "string", //string or numeric
43+
'name' => 'is_numeric',
44+
'pattern' => '/is_numeric/',
45+
'pattern_check' => '/is_numeric/',
46+
'pattern_type' => 'string',
47+
'not_numeric_rule' => 'is_numeric is a string rule'
48+
],
49+
'/required/' => [
50+
'parameter' => false,
51+
"parameter_type" => "string", //string or numeric
52+
'name' => 'required',
53+
'pattern' => '/required/',
54+
'pattern_check' => '/required/',
55+
'pattern_type' => 'string',
56+
'not_numeric_rule' => 'required is a string rule'
57+
],
3858

3959
];
4060

src/validation.library.php

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ private static function checkRules($data,$rule) : bool{
2424
if (preg_match($pattern, $rule)){
2525

2626
$rulePattern = self::$rule_pattern_check[$pattern];
27-
27+
$numerical = 0;
28+
$checkStatus = true;
2829

2930

3031
if (!preg_match($rulePattern['pattern'], $rule, $matches)){
@@ -33,46 +34,59 @@ private static function checkRules($data,$rule) : bool{
3334
}
3435

3536
if ($rulePattern['parameter']){
36-
3737
preg_match($rulePattern['pattern_check'] ,$rule, $matches);
38-
39-
if (!is_numeric($matches[1])){
40-
self::$library_errors[] = $rulePattern['not_numeric_rule'];
41-
return false;
38+
if($rulePattern['parameter_type'] == "numeric"){
39+
if (!is_numeric($matches[1])){
40+
self::$library_errors[] = $rulePattern['not_numeric_rule'];
41+
return false;
42+
}
4243
}
44+
$numerical = $matches[1];
45+
}
4346

4447

48+
if($rulePattern['pattern_type'] == "numeric"){
49+
//numeric check
4550

46-
if($rulePattern['pattern_type'] == "numeric"){
47-
//numeric check
48-
$numerical = $matches[1];
4951

50-
$checkStatus = true;
52+
if ($rulePattern['name'] == 'max_length'){
53+
if (strlen($data['value']) > $numerical && $numerical > 0){
54+
$checkStatus = false;
55+
}
56+
}if ($rulePattern['name'] == 'min_length') {
57+
if (strlen($data['value']) < $numerical && $numerical > 0) {
58+
$checkStatus = false;
59+
}
60+
}
61+
}
62+
else if($rulePattern['pattern_type'] == "string"){
63+
//string check
64+
if ($rulePattern['name'] == 'is_numeric'){
65+
if (!is_numeric($data['value'])){
66+
$checkStatus = false;
67+
}
68+
}
69+
if ($rulePattern['name'] == 'required'){
70+
if (empty($data['value'])){
71+
$checkStatus = false;
72+
}
73+
}
74+
5175

76+
}
5277

5378

54-
if ($rulePattern['name'] == 'max_length'){
55-
if (strlen($data['value']) > $numerical){
56-
$checkStatus = false;
57-
}
58-
}if ($rulePattern['name'] == 'min_length') {
59-
if (strlen($data['value']) < $numerical) {
60-
$checkStatus = false;
61-
}
79+
if($checkStatus === false){
80+
//self::$errors['status'] = 0;
81+
if (isset(self::$errorMessages[$data['name']][$rulePattern['name']])){
82+
self::$errors[] = self::$errorMessages[$data['name']][$rulePattern['name']];
83+
}else{
84+
self::$errors[] = str_replace(':field', $data['name'], self::$error_messages[$rulePattern['name']]);
6285
}
86+
}
6387

6488

6589

66-
if($checkStatus === false){
67-
self::$errors['status'] = 0;
68-
if (isset(self::$errorMessages[$data['name']]['min_length'])){
69-
self::$errors[] = self::$errorMessages[$data['name']]['min_length'];
70-
}else{
71-
self::$errors[] = str_replace(':field', $data['name'], self::$error_messages[$rulePattern['name']]);
72-
}
73-
}
74-
}
75-
}
7690
}
7791
}
7892

@@ -135,7 +149,8 @@ public static function validateRule($data, $rules) : array {
135149
foreach(self::$rules as $key => $value){
136150
foreach($value as $k => $v) {
137151
//check if the rule has a parameter
138-
$rule = self::checkRules(['name' => $key, 'value' => $data[$key]], $v);
152+
153+
$rule = self::checkRules(['name' => $key, 'value' => ($data[$key] ?? null)] , $v);
139154
if (!$rule){
140155
return self::$library_errors;
141156
}

0 commit comments

Comments
 (0)