PHPackages                             gp/validator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. gp/validator

ActiveLibrary

gp/validator
============

A validator package for php web application

1.0.7(9mo ago)0292MITPHPPHP ^8.1CI passing

Since Mar 17Pushed 9mo agoCompare

[ Source](https://github.com/periyandavar/gp_validator)[ Packagist](https://packagist.org/packages/gp/validator)[ RSS](/packages/gp-validator/feed)WikiDiscussions main Synced today

READMEChangelog (6)Dependencies (5)Versions (23)Used By (0)

GP Validator Library
====================

[](#gp-validator-library)

This library provides a robust validation engine to validate data inputs based on various rules. The library is modular and easily extensible, allowing you to create and use custom validation rules. which has various built in validation rules for common use cases (e.g., email, mobile number, numeric validation) also support for custom validation rules.

Table of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Features](#features)
- [Classes](#classes)
    - [Field](#field)
    - [Fields](#fields)
    - [ValidationEngine](#validationengine)
    - [validationRule](#validationengine)
- [Usage](#usage)
    - [Creating a Validation Field](#1-creating-a-validation-field)
    - [Validating a Single Field](#2-validating-a-single-field)
    - [Grouping Fields for Validation](#3-grouping-fields-for-validation)
    - [Validating All Fields in a Group](#4-validating-all-fields-in-a-group)
        - [using the fields](#using-the-fields)
        - [Using the ValidationEngine](#using-the-validationengine)
    - [Built-in Validation Rules](#5-built-in-validation-rules)
    - [Creating Custom Validation Rules](#6-creating-custom-validation-rules)
- [Example](#example)
- [Contributing](#contributing)
- [License](#license)
- [Author](#author)
- [Contact](#contact)

---

Requirements
------------

[](#requirements)

- PHP 7.3.5 or higher
- composer

---

Installation
------------

[](#installation)

You can install `gp_validator` using Composer. Run the following command in your terminal:

```
composer require gp/validator

```

---

Getting Started
---------------

[](#getting-started)

After installation, you can start using the package by including the autoloader:

```
require 'vendor/autoload.php';

```

---

Features
--------

[](#features)

- Field-Based Validation: Validate individual fields with customizable rules and error messages.
- Add Multiple rule for the field: Ability to define multiple rules for a single field.
- Supports field-level and group-level validations.
- Bulk Validation: Validate multiple fields at once using the Fields collection.
- Predefined Validation Rules: Out-of-the-box support for common validation rules such as:
    - Email validation
    - Numeric validation
    - Mobile number validation
    - Regular expression-based validation
    - Length validation
- Custom Validation Rules: Extend the validation engine with your own rules by implementing the ValidationRule interface.
- Dynamic Rule Assignment: Assign different validation rules dynamically to fields at runtime.
- Error Management: Retrieve detailed error messages for invalid fields during validation.
- Dependency-Fre: Lightweight and does not depend on external libraries, ensuring easy integration with any PHP project.
- PSR-4 Compatible: Fully compatible with PSR-4 autoloading standards for seamless integration into modern PHP applications.
- Extensible Architecture: Designed with extensibility in mind, allowing developers to easily add features or customize behavior.
- Developer-Friendly API: Intuitive and easy-to-use API for managing fields, rules, and validations.
- Validation Continuation: Optional configuration to continue validating other rules even if one rule fails.
- Regex Support: Built-in support for regular expression-based validations for flexible rule definition.
- Field-Level Rule Chaining: Support for chaining multiple validation rules for a single field.
- Error Aggregation: Collect and display validation errors from all fields in a single operation.

---

Classes
-------

[](#classes)

### Field

[](#field)

#### Methods

[](#methods)

MethodDescription`getName()`Returns the field's name.`getData()`Returns the field's data.`getRules()`Returns the validation rules associated with the field.`getErrors()`Returns the errors for the field.`validate()`Validates the field using the applied rules.`addRule(string $rule)`Adds a validation rule to the field.`addError(string $error)`Adds an error message to the field.---

### Fields

[](#fields)

#### Methods

[](#methods-1)

MethodDescription`addField(Field $field)`Adds a single field to the collection.`addFields(Field ...$fields)`Adds multiple fields to the collection.---

### ValidationEngine

[](#validationengine)

#### Methods

[](#methods-2)

MethodDescription`validate(Fields $fields)`Validates all fields in the group.`validateField(Field $field)`Validates a single field.---

### ValidationRule

[](#validationrule)

#### Method

[](#method)

MethodDescriptionvalidate(string $data, string &amp;$msg): ?boolValidates the provided data and Returns resulthe method also populatesthe $msg parameter with an error message if validation fails.Usage
-----

[](#usage)

### 1. Creating a Validation Field

[](#1-creating-a-validation-field)

To validate a single field, create an instance of the `Field` class:

```
use Validator\Field\Field;

$field = new Field('email', 'user@example.com', ['emailValidation']);

```

### 2. Validating a Single Field

[](#2-validating-a-single-field)

Use the `validate()` method on a `Field` instance:

```
$isValid = $field->validate();

if ($isValid) {
  echo "The field is valid.";
} else {
  echo "The field is invalid. Errors: " . implode(', ', $field->getErrors());
}

```

### 3. Grouping Fields for Validation

[](#3-grouping-fields-for-validation)

You can group multiple fields using the `Fields` class:

```

use Validator\Field\Fields;

$fields = new Fields([
    new Field('email', 'user@example.com', ['emailValidation']),
    new Field('mobile', '9876543210', ['mobileNumberValidation']),
]);

```

### 4. Validating All Fields in a Group

[](#4-validating-all-fields-in-a-group)

#### using the `fields`

[](#using-the-fields)

```
$isValid = $fields->validate();

if ($isValid) {
  echo "All fields are valid.";
} else {
  print_r($fields->getErrors()); // print errors
  print_r($fields->getInvalidFields());// print invalidFieldDetails
}

```

#### Using the `ValidationEngine`

[](#using-the-validationengine)

```
use Validator\ValidationEngine;

$validationEngine = new ValidationEngine(); $invalidFieldDetails = [];
$isValid = $validationEngine->validate($fields, true, $invalidFieldDetails);

if ($isValid) {
  echo "All fields are valid.";
} else {
  print_r($invalidFieldDetails); // View details of invalid fields
}

```

### 5. Built-in Validation Rules

[](#5-built-in-validation-rules)

The following validation rules are included in the library:

Rule NameDescription`emailValidation`Validates that the field contains a valid email.`mobileNumberValidation`Validates that the field contains a valid mobile number in India.`landlineValidation`Validates Indian landline numbers.`alphaSpaceValidation`Validates that the field contains only alphabets and spaces.`isbnValidation`Validates that the field contains a valid ISBN-10.`numericValidation`Validates that the field contains a numeric value and optionally checks a range.`positiveNumberValidation`Validates that the field contains a positive number.`regexValidation`Validates data using a custom regular expression.`lengthValidation`Validates the length of the data. Supports min and max length.`requiredValidation`Validates that the field is not empty.### 6. Creating Custom Validation Rules

[](#6-creating-custom-validation-rules)

To create a custom validation rule, implement the `ValidationRule` interface:

```

use System\Library\ValidationRule;

class CustomRule implements ValidationRule
{
    public function validate(string $data, string &$msg): ?bool
    {
        if ($data === 'custom') {
            return true;
        }

$msg = "The value must be 'custom'.";
        return false;
    }
}

```

Then, apply the custom rule to a field:

```
$field = new Field('example', 'custom', [CustomRule::class]); $field->validate();

```

---

Example
-------

[](#example)

Here's a full example of validating multiple fields:

```
use Validator\Field\Field;
use Validator\Field\Fields;
use Validator\ValidationEngine;

$fields = new Fields([
    new Field('email', 'user@example.com', ['emailValidation']),
    new Field('age', 25, ['numericValidation']),
]);

$validationEngine = new ValidationEngine(); $invalidFields = [];
$isValid = $validationEngine->validate($fields, true, $invalidFields);

if ($isValid) {
  echo "All fields are valid.";
} else {
  print_r($invalidFields); // Display invalid field details
}

```

---

Contributing
------------

[](#contributing)

Contributions are welcome! If you would like to contribute to gp\_validator, please follow these steps:

- Fork the repository.
- Create a new branch (git checkout -b feature/- YourFeature).
- Make your changes and commit them (git commit -m 'Add some feature').
- Push to the branch (git push origin feature/YourFeature).
- Open a pull request.
- Please ensure that your code adheres to the coding standards and includes appropriate tests.

---

License
-------

[](#license)

This package is licensed under the MIT License. See the [LICENSE](https://github.com/periyandavar/gp_validator/blob/main/LICENSE) file for more information.

---

Contact
-------

[](#contact)

For questions or issues, please reach out to the development team or open a ticket.

---

Author
------

[](#author)

- Periyandavar [Github](https://github.com/periyandavar) ()

---

###  Health Score

35

—

LowBetter than 81% of packages

Maintenance56

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Recently: every ~2 days

Total

9

Last Release

296d ago

Major Versions

v0.0.1 → v1.0.02025-03-22

PHP version history (2 changes)v1.0.0PHP ^8.0

1.0.3PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/b9f29960ccfedb3111fb3b6869ff5449da9cd7d027b19edfdef9d17a0915cb7c?d=identicon)[periyandavar](/maintainers/periyandavar)

---

Top Contributors

[![periyandavar](https://avatars.githubusercontent.com/u/209335631?v=4)](https://github.com/periyandavar "periyandavar (45 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/gp-validator/health.svg)

```
[![Health](https://phpackages.com/badges/gp-validator/health.svg)](https://phpackages.com/packages/gp-validator)
```

###  Alternatives

[lastzero/test-tools

Increases testing productivity by adding a service container and self-initializing fakes to PHPUnit

2244.0k13](/packages/lastzero-test-tools)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
