hybridmind / validate
A robust and flexible validation library for PHP.
1.0.0
2024-11-05 12:09 UTC
Requires
- php: >=7.4
README
The Validate
class offers a robust and flexible way to handle validation logic in your PHP applications. This class simplifies and centralizes validation rules, making your code cleaner and more maintainable.
Features
- Simplified Code: Reduce boilerplate and repetitive validation logic.
- Centralized Validation Rules: Manage all your validation logic in one place.
- Improved Readability: Declarative syntax for clearer, more maintainable code.
- Extensible: Easily add new validation rules as needed.
Before and After
Before Manual validation logic often leads to repetitive code:
if (empty($_POST['username'])) { $errors[] = 'Username is required.'; } elseif (strlen($_POST['username']) < 3) { $errors[] = 'Username must be at least 3 characters long.'; } elseif (strlen($_POST['username']) > 20) { $errors[] = 'Username cannot exceed 20 characters.'; }
After Using the Validate class streamlines the process:
$validate = new Validate(); $validationResult = $validate->check($_POST, [ 'username' => [ Validate::REQUIRED => ['message' => 'Username is required.'], Validate::MIN => ['min' => 3, 'message' => 'Username must be at least 3 characters long.'], Validate::MAX => ['max' => 20, 'message' => 'Username cannot exceed 20 characters.'], ], ]); if ($validate->isValid()) { // Proceed with processing } else { $errors = $validate->getErrors(); // Handle validation errors }
Installation
To use the Validate
class, simply include the class file in your project.
composer require hybridmind/validate
Contributing
We welcome contributions to enhance the Validate class. To contribute, please follow these steps:
- Fork the repository.
- Create a new branch.
- Make your changes and commit them with clear and descriptive messages.
- Push your changes to the branch.
- Submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.