DEV Community

soheil-khaledabadi
soheil-khaledabadi

Posted on

Introducing php-smart-validator : A Lightweight, Intuitive PHP Validation Package

Introduction

Validating data is a crucial part of any web application. Whether you're developing a small website or a large-scale enterprise application, you need to ensure that your data is clean, consistent, and in the right format. Today, I'm excited to introduce php-smart-validator, a lightweight and intuitive PHP package designed to simplify data validation.

In this post, I’ll walk you through the features of php-smart-validator, how to install it, and provide a few examples to get you started!


Why php-smart-validator?

With so many PHP validation libraries out there, you might wonder why I decided to create another one. The answer is simple: ease of use and flexibility. php-smart-validator is designed to provide a minimalistic yet powerful way to handle data validation without the bloat. It aims to reduce boilerplate code and improve readability while maintaining high flexibility and customizability.

Key Features:

  1. Simple API: Easy to use and understand, making validation quick to implement.
  2. Lightweight: The package is minimal, ensuring it doesn’t bloat your application.
  3. Flexible Rules: Supports a variety of validation rules, and allows for custom rule creation.
  4. Custom Error Messages: Provides detailed and customizable error messages.
  5. Chainable Rules: Allows chaining of multiple rules for a single data point for better readability.

Getting Started

To start using php-smart-validator, you’ll need to install it via Composer. Run the following command in your terminal:

composer require geekgrove/php-smart-validator
Enter fullscreen mode Exit fullscreen mode

Once installed, you can start validating data right away!


Basic Usage

Here’s a quick example of how you can use php-smart-validator to validate user input:

require 'vendor/autoload.php';

use GeekGrove\Validator\Validator;

// Sample data to validate
$data = [
    'username' => 'john_doe',
    'email' => 'john.doe@example.com',
    'age' => 25,
];

// Define validation rules
$rules = [
    'username' => 'required|alpha_num|min:3|max:15',
    'email' => 'required|email',
    'age' => 'required|integer|min:18',
];

// Create a new validator instance
$validator = new Validator($data, $rules);

// Run validation
if ($validator->fails()) {
    // Get validation errors
    $errors = $validator->errors();
    print_r($errors);
} else {
    echo "Validation passed!";
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we first define an array of data to validate and then set up our rules. The Validator class takes care of the rest. If any of the data fails to pass the rules, the fails method returns true, and you can retrieve the error messages using the errors method.

Custom Validation Rules

One of the powerful features of php-smart-validator is the ability to create custom validation rules. Here’s how you can add a custom rule to check if a username is unique:

$validator->addRule('unique_username', function ($field, $value, $parameters) {
    // Check if username exists in database (example logic)
    return !User::where('username', $value)->exists();
}, 'The :attribute has already been taken.');

$rules['username'] .= '|unique_username';

if ($validator->fails()) {
    // Handle validation errors
}
Enter fullscreen mode Exit fullscreen mode

With this approach, you can tailor the validation process to suit the specific needs of your application, ensuring data integrity while providing clear feedback to users.

Custom Error Messages

php-smart-validator allows you to customize the error messages for each rule, making your validation process user-friendly and more informative:

$messages = [
    'username.required' => 'Please enter a username.',
    'email.email' => 'The email address format is invalid.',
];

$validator = new Validator($data, $rules, $messages);
Enter fullscreen mode Exit fullscreen mode

Chaining Rules

You can chain multiple rules for better readability and management:

$rules = [
    'username' => Validator::rule('required')->rule('alpha_num')->rule('min', 3)->rule('max', 15),
];
Enter fullscreen mode Exit fullscreen mode

Conclusion

php-smart-validator is a lightweight, flexible, and easy-to-use PHP validation package designed to simplify your development workflow. Whether you're a beginner or an experienced developer, you'll find it invaluable for ensuring data integrity and consistency in your projects.

Give php-smart-validator a try today, and let me know what you think! You can find the package on GitHub at geekGroveOfficial/php-smart-validator. Contributions, feedback, and stars are all welcome!

Happy coding!


Follow me for more updates on GitHub

Top comments (0)