Update: 2026-05-08 04:58:23
This commit is contained in:
@@ -13,12 +13,36 @@ final class Validator
|
||||
{
|
||||
$errors = [];
|
||||
foreach ($rules as $field => $rule) {
|
||||
if (str_contains($rule, 'required') && (empty($data[$field]) && $data[$field] !== '0')) {
|
||||
$value = $data[$field] ?? null;
|
||||
|
||||
if (str_contains($rule, 'required') && (empty($value) && $value !== '0')) {
|
||||
$errors[$field] = "The {$field} field is required.";
|
||||
continue; // Skip further rules if required field is missing
|
||||
}
|
||||
if (str_contains($rule, 'email') && !empty($data[$field]) && !filter_var($data[$field], FILTER_VALIDATE_EMAIL)) {
|
||||
|
||||
if (str_contains($rule, 'email') && !empty($value) && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[$field] = "The {$field} must be a valid email address.";
|
||||
}
|
||||
|
||||
// Password strength: min 8 chars, at least 1 uppercase, 1 lowercase, 1 digit
|
||||
if (str_contains($rule, 'strong_password') && !empty($value)) {
|
||||
if (strlen($value) < 8) {
|
||||
$errors[$field] = 'كلمة المرور يجب أن تكون 8 أحرف على الأقل.';
|
||||
} elseif (!preg_match('/[A-Z]/', $value)) {
|
||||
$errors[$field] = 'كلمة المرور يجب أن تحتوي على حرف كبير واحد على الأقل.';
|
||||
} elseif (!preg_match('/[a-z]/', $value)) {
|
||||
$errors[$field] = 'كلمة المرور يجب أن تحتوي على حرف صغير واحد على الأقل.';
|
||||
} elseif (!preg_match('/[0-9]/', $value)) {
|
||||
$errors[$field] = 'كلمة المرور يجب أن تحتوي على رقم واحد على الأقل.';
|
||||
}
|
||||
}
|
||||
|
||||
// Generic min length: min:8
|
||||
if (preg_match('/min:(\d+)/', $rule, $m) && !empty($value)) {
|
||||
if (mb_strlen($value) < (int)$m[1]) {
|
||||
$errors[$field] = "The {$field} must be at least {$m[1]} characters.";
|
||||
}
|
||||
}
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user