Initial commit
This commit is contained in:
24
app/Core/Validator.php
Normal file
24
app/Core/Validator.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
class Validator {
|
||||
public static function validate(array $data, array $rules) {
|
||||
$errors = [];
|
||||
foreach ($rules as $field => $rule) {
|
||||
if (strpos($rule, 'required') !== false && empty($data[$field])) {
|
||||
$errors[$field] = ucfirst($field) . " is required.";
|
||||
}
|
||||
if (strpos($rule, 'email') !== false && !filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[$field] = "Invalid email format.";
|
||||
}
|
||||
if (strpos($rule, 'min:8') !== false && strlen($data[$field] ?? '') < 8) {
|
||||
$errors[$field] = ucfirst($field) . " must be at least 8 characters.";
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user