-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.php
199 lines (183 loc) · 6.66 KB
/
validation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Here I have developed a custom validation for WS request param or WS rules.
*/
class Validation
{
// Uncomment line for a use of custom lable in error message.
public $customMsg = ["first_name"=>"First Name","email"=>"Email","contact"=>"Contact Number"];
// and please uncomment $msg variable for custom message and comment olg $msg
public $conn;
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Create a connection with database.
*/
function __construct(){
$host = "localhost";
$username = "root";
$password = "ln";
$database = "mydb";
$this->conn = mysqli_connect($host,$username,$password,$database);
if(!$this->conn){
echo "Database Connection Error";
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : This is main method. That execute both array request_data and rules
*/
public function checkValidation($request_data,$rules){
// Main loop that traverse a request_data values.
foreach ($request_data as $key => $value) {
// check for which field rules is define.
if(array_key_exists($key,$rules)){
// if one filed have multiple validation then explode validation methods.
$validationRule = explode("|",$rules[$key]);
// call that all validation method.
foreach ($validationRule as $methodName) {
$dbValidation = explode(":",$methodName);
if(sizeof($dbValidation)>=2){
// mendetory fields
// print_r($dbValidation);
$methodName = $dbValidation[0];
$table = $dbValidation[1];
$fieldName = $key;
// optional fields
$uniqueFieldName = (isset($dbValidation[2]) && !empty($dbValidation[2])?$dbValidation[2]:"");
$id = (isset($dbValidation[3]) && !empty($dbValidation[3])?$dbValidation[3]:"");
$returnData = $this->$methodName($value,$fieldName,$table,$uniqueFieldName,$id);
}else {
// make a validation rule name as a function name.
$returnData = $this->$methodName($value,$key);
}
//if error so it return json error message.
if($returnData!=$value){
// return a error message.
return $returnData;
}
}
}
}
// if all rules are setisfying conditions then return true.
return true;
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Check a given email is valid or not.
*/
public function email($value,$fieldName){
if(filter_var($value,FILTER_VALIDATE_EMAIL) === false){
// to send error message we are calling a Formatter class's method for display alert and error with their message.
$msg = "Invalide $fieldName address";
// $msg = "Invalide ".$this->customMsg[$fieldName]." address";
return $this->errorAlertMessage($msg);
}else {
return $value;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Check given param is empty or not. Work for a required value
*/
public function required($value,$fieldName){
if(trim($value)==""){
// Uncomment line for a use of custom lable in error message.
// $msg = "Please enter ".$this->customMsg[$fieldName];
$msg = "Please enter ".str_replace("_"," ",$fieldName);
return $this->errorAlertMessage($msg);
}else {
return $value;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Here preg match for a contact number. You can modify as per your requirement.
*/
public function contactNumber($value,$fieldName){
if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $value))
{
$msg = "Invalid ".str_replace("_"," ",$fieldName);
// $msg = "Invalid ".$this->customMsg[$fieldName];
return $this->errorAlertMessage($msg);
}else {
return $value;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : That is used to restrict a duplicate value into database.
*/
// That check a data value must be unique at the time of insert data into database.
public function unique($value,$fieldName,$table,$uniqueFieldName="",$id=""){
// That condtion mostly append at the time of update data.
$extraCondition = "";
if(!empty($uniqueFieldName) && !empty($id)){
$extraCondition = " and ".$uniqueFieldName."!='".$id."'";
}
$query = "SELECT id from ".$table." WHERE ".$fieldName."='".$value."'".$extraCondition."";
$result = $this->conn->query($query);
if($result->num_rows>0){
$msg = str_replace("_"," ",$fieldName)." is already taken";
// $msg = $this->customMsg[$fieldName]." is already taken";
return $this->errorAlertMessage($msg);
}else {
return $value;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Check a given input for the parameter is an integer or not.
*/
public function integer($value,$fieldName){
if(!preg_match('/^[0-9]*$/',$value))
{
$msg = str_replace("_"," ",$fieldName)." must be number";
// $msg = $this->customMsg[$fieldName]." must be number";
return $this->errorAlertMessage($msg);
}else {
return $value;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : Check requested parameters and expected parameters are same or not.
*/
public function isValidRequest($requiredRequestFields,$request_data){
$result = array_diff_key($requiredRequestFields,$request_data);
if(sizeof($result)!=0){
$msg = implode(" & ",array_keys($result))." parameters are missing in JSON request";
return $this->errorAlertMessage($msg);
}else {
return true;
}
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : If any success message you want to send as response that will formated here.
*/
public function successDataFormat($request_data){
$data['data'] = $request_data;
return json_encode($data);
}
/**
* Developer : Siddharajsinh Zala.
* Contact: zalasid.mca@gmail.com / 834 76 76 729
* Description : If any error message you want to send as response that will formated here.
*/
public function errorAlertMessage($msg){
$msg = array("error"=>array("message"=>$msg));
return json_encode($msg);
}
}
?>