Validate
The Validate
class in PHPFast is responsible for validating user input using the Respect\Validation library. It provides a collection of methods to check various types of data, such as strings, numbers, dates, emails, and even JSON or IP addresses.
This class helps ensure that data meets specific criteria before being processed or stored in a database, reducing errors and improving security.
Methods in Validate.php
Validate.php
String & Character Validations
// Ensures the string contains only letters (A-Z, a-z).
public static function alpha() {
return RespectValidator::alpha();
}
// Ensures the string contains only numeric digits (0-9).
public static function digit() {
return RespectValidator::digit();
}
// Ensures the string contains only lowercase letters.
public static function lowercase() {
return RespectValidator::lowercase();
}
// Ensures the string contains only uppercase letters.
public static function uppercase() {
return RespectValidator::uppercase();
}
Example:
Validate::alpha()->validate("hello"); // True
Validate::digit()->validate("1234"); // True
Validate::uppercase()->validate("HELLO"); // True
Validate::lowercase()->validate("Hello"); // False
Number & Range Validations
// Ensures the value is a valid number.
public static function NumericVal() {
return RespectValidator::NumericVal();
}
// Ensures the value falls within a specific range.
public static function between($min, $max) {
return RespectValidator::between($min, $max);
}
Example:
Validate::NumericVal()->validate(25); // True
Validate::between(10, 50)->validate(30); // True
Validate::between(10, 50)->validate(5); // False
Email, URL, and IP Validations
// Validates an email format.
public static function email() {
return RespectValidator::email();
}
// Checks if the value is a valid URL.
public static function url() {
return RespectValidator::url();
}
// Validates an IP address.
public static function ip() {
return RespectValidator::ip();
}
Example:
Validate::email()->validate("[email protected]"); // True
Validate::url()->validate("https://example.com"); // True
Validate::ip()->validate("192.168.1.1"); // True
Validate::email()->validate("invalid-email"); // False
Password & Security Validations
// Ensures the value is not empty.
public static function notEmpty() {
return RespectValidator::notEmpty();
}
// Ensures the value is exactly equal to another.
public static function equals($compareTo) {
return RespectValidator::equals($compareTo);
}
Example:
Validate::notEmpty()->validate("password"); // True
Validate::equals("admin")->validate("admin"); // True
Validate::equals("admin")->validate("user"); // False
JSON, UUID, and Special Format Validations
// Ensures the string is a valid JSON format.
public static function json() {
return RespectValidator::json();
}
// Ensures the string is a valid UUID.
public static function uuid() {
return RespectValidator::uuid();
}
// Validates credit card numbers.
public static function creditCard() {
return RespectValidator::creditCard();
}
Example:
Validate::json()->validate('{"name": "John"}'); // True
Validate::uuid()->validate("550e8400-e29b-41d4-a716-446655440000"); // True
Validate::creditCard()->validate("4111111111111111"); // True
Validating an Array of Data (check()
)
check()
)Validates multiple fields based on predefined rules.
Stores errors for fields that fail validation.
Example:
$data = [
'username' => 'john_doe',
'email' => 'invalid-email'
];
$rules = [
'username' => [
'rules' => [Validate::alnum(), Validate::length(3, 20)],
'messages' => ['Username must be alphanumeric.', 'Length must be between 3 and 20 characters.']
],
'email' => [
'rules' => [Validate::email()],
'messages' => ['Invalid email format.']
]
];
$validator = new Validate();
if (!$validator->check($data, $rules)) {
print_r($validator->getErrors()); // Output validation errors
}
Last updated
Was this helpful?