-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
60 lines (54 loc) · 1.43 KB
/
Copy pathfunctions.php
File metadata and controls
60 lines (54 loc) · 1.43 KB
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
<?php
/**
* Trim whitespace and strip tags from data
*
* @param array $data form data
* @param array $allowedData allowed fields
* @return array
*/
function filterData($data, $allowedData)
{
foreach ($data as $key => $value) {
if (!in_array($key, $allowedData)) {
unset($data[$key]);
continue;
}
$data[$key] = trim(strip_tags($value));
}
return $data;
}
/**
* Validate form data
*
* @param array $data form data
* @param array $rules validation rules
* @return array $errors
*/
function validate($data, $rules)
{
$errors = [];
foreach ($data as $key => $value) {
switch ($rules[$key]) {
case 'required':
if (empty($value)) {
$errors[$key] = sprintf('%s is required', $key);
}
break;
case 'number':
if (!is_numeric($value)) {
$errors[$key] = sprintf('%s must be a number', $key);
}
break;
case 'email':
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$errors[$key] = 'Email is not valid';
}
break;
case 'phone':
if (!preg_match('/^\+421[0-9]{9}/', $value)) {
$errors[$key] = 'Phone is not valid';
}
}
}
return $errors;
}