Filip Rojek
21c2f4598b
All checks were successful
Build and Deploy Zola Website / build_and_deploy (push) Successful in 13s
69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
class Validator {
|
|
private $errors = [];
|
|
|
|
/**
|
|
* Check if a field is not empty
|
|
*/
|
|
public function required($field, $value, $message = null) {
|
|
if (empty(trim($value))) {
|
|
$this->errors[$field] = $message ?? "$field is required.";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a field contains numbers
|
|
*/
|
|
public function number($field, $value, $message = null) {
|
|
if(!is_numeric($value)) {
|
|
$this->errors[$field] = $message ?? "$field must be an number";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a field meets minimum length
|
|
*/
|
|
public function minLength($field, $value, $length, $message = null) {
|
|
if (strlen($value) < $length) {
|
|
$this->errors[$field] = $message ?? "$field must be at least $length characters.";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a field contains letters and numbers
|
|
*/
|
|
public function alphanumeric($field, $value, $message = null) {
|
|
if (!preg_match('/^(?=.*[a-zA-Z])(?=.*\d).+$/', $value)) {
|
|
$this->errors[$field] = $message ?? "$field must contain letters and numbers.";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate an email
|
|
*/
|
|
public function email($field, $value, $message = null) {
|
|
if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
|
|
$this->errors[$field] = $message ?? "Invalid email format.";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get validation errors
|
|
*/
|
|
public function errors() {
|
|
return $this->errors;
|
|
}
|
|
|
|
public function setErrors($errors) {
|
|
$this->errors = $errors;
|
|
}
|
|
|
|
/**
|
|
* Check if validation passed
|
|
*/
|
|
public function passes() {
|
|
return empty($this->errors);
|
|
}
|
|
}
|