Deploy: 2026-05-21 01:26:06
This commit is contained in:
@@ -19,31 +19,8 @@ class BaseController
|
||||
*/
|
||||
protected function validate(Request $request, array $rules): array
|
||||
{
|
||||
$errors = [];
|
||||
$data = $request->getBody();
|
||||
|
||||
foreach ($rules as $field => $constraints) {
|
||||
$value = $data[$field] ?? null;
|
||||
$constraintsArray = explode('|', $constraints);
|
||||
|
||||
foreach ($constraintsArray as $constraint) {
|
||||
if ($constraint === 'required') {
|
||||
if ($value === null || $value === '') {
|
||||
$errors[$field][] = "The {$field} field is required.";
|
||||
}
|
||||
} elseif ($constraint === 'email') {
|
||||
if ($value !== null && $value !== '' && !filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
||||
$errors[$field][] = "The {$field} must be a valid email address.";
|
||||
}
|
||||
} elseif (strpos($constraint, 'min:') === 0) {
|
||||
$min = (int) substr($constraint, 4);
|
||||
if ($value !== null && strlen((string)$value) < $min) {
|
||||
$errors[$field][] = "The {$field} must be at least {$min} characters.";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
$validator = new \App\Core\Validator();
|
||||
$validator->validate($request->getBody(), $rules);
|
||||
return $validator->getErrors();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user