PHPackages                             ericsnguyen/php-validation - 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. ericsnguyen/php-validation

ActiveLibrary

ericsnguyen/php-validation
==========================

a validation form like other language c#, java with annotations

24PHP

Since Jan 5Pushed 5y agoCompare

[ Source](https://github.com/thicongtu2/php-validation)[ Packagist](https://packagist.org/packages/ericsnguyen/php-validation)[ RSS](/packages/ericsnguyen-php-validation/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP validation
==============

[](#php-validation)

This is a validation form in JAVA. we mark the field with an `Annotation` to constraint it.

In some other language like c# and java we use `Attribute` a.k.a `Annotation` to mark the field.

```
import javax.validation.constraints.AssertTrue;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.validation.constraints.Email;

public class User {

    @NotNull(message = "Name cannot be null")
    private String name;

    @AssertTrue
    private boolean working;

    @Size(min = 10, max = 200, message
      = "About Me must be between 10 and 200 characters")
    private String aboutMe;

    @Min(value = 18, message = "Age should not be less than 18")
    @Max(value = 150, message = "Age should not be greater than 150")
    private int age;

    @Email(message = "Email should be valid")
    private String email;
```

It is very elegant, short and beautiful. I do a version of it on PHP.

### Prerequisites

[](#prerequisites)

Your web application should use a DI library to manage all object. (Eg: Laravel Framework...) it will native support without adding any code, just define the setting datetime for it.

USAGE
-----

[](#usage)

### step 1: setting the datetime format.

[](#step-1-setting-the-datetime-format)

```
 \Validation\Configuration::setting([
            'date_format'=>'Y-m-d',
        ]);
```

### step 2: define form class.

[](#step-2-define-form-class)

```
class HiRequest extends \Validation\BaseRequest{
    /**
    * @\Validation\Annotations\NotNull
    * @\Validation\Annotations\MaxLength(max=256)
    */
    public string $name;

    /**
    * @\Validation\Annotations\Range(min=18, max=50)
    */
    public int $age;
}
```

That is OK!!

### We support some validation:

[](#we-support-some-validation)

- NotNull
- Min
- Max
- Email
- Match
- MinLength
- MaxLength
- Range
- Length

You can create your own constraint annotation by implement `IValidator` interface

```
Class YourCustomConstraint extends \Doctrine\Common\Annotations\Annotation implements \Validation\Interfaces\IValidator {

    public function check($value) : bool{
        // predicate code
        return true;
    }

    public function getMessage() : string{
       return "what message you want!!!";
    }
}
```

### For old style.

[](#for-old-style)

If you don't use DI library, at the block code that resolve controller instance. you must resolve it like below.

- i assume your controller is `HelloController`

```
class HelloController{
    function sayHi(HiRequest $hiRequest){

    }
}
```

- your `HiRequest`

```
class HiRequest extends \Validation\BaseRequest{
    /**
    * @\Validation\Annotations\NotNull
    * @\Validation\Annotations\MaxLength(max=256)
    */
    public string $name;
    ...
}
```

- let say it is your code that resolve the controller and method that handle the request `hello/sayHi`.

```
$controller = new HelloController();
$controller->sayHi(new HiRequest);
```

- Add the function that get parameters of `sayHi` function:

```
    public function getParameterOfActionMethod($className, $methodName){
        $class = new ReflectionClass($className);
        $method = $class->getMethod($methodName);
        $parameters = [];
        $actionParameters= $method->getParameters();
        foreach ($actionParameters as $parameter){
            $parameterType = (string)$parameter->getType();
            if (class_exists($parameterType)){
                $parameterInstance = new $parameterType();
                array_push($parameters, $parameterInstance);
            }else{
                // must be scalar type
                $value = $_REQUEST($parameter->name);
                if ($value==null){
                    $value = $_REQUEST(StringUtils::camelToSnake($parameter->name));
                }
                array_push($parameters, $value);
            }
        }
        return $parameters;
    }
// at controller handle code:
$controller = new HelloController();
$controller->sayHi(...$this->getParameterOfActionMethod(HelloController::class, "sayHi"));
```

- Enjoy it!!

```
class HelloController{
    function sayHi(HiRequest $hiRequest){
        $hiRequest->name ... // validated, and do your logic.
    }
}
```

- How to contribute - any contribution is welcome!!

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2dcf63ef46261a94307b7e2b87fb2d2973ee4bc7f6e5705dd0f7fd5f5ddf9b9d?d=identicon)[ericsnguyen](/maintainers/ericsnguyen)

---

Top Contributors

[![anhngocnguyen94](https://avatars.githubusercontent.com/u/67720617?v=4)](https://github.com/anhngocnguyen94 "anhngocnguyen94 (7 commits)")

### Embed Badge

![Health badge](/badges/ericsnguyen-php-validation/health.svg)

```
[![Health](https://phpackages.com/badges/ericsnguyen-php-validation/health.svg)](https://phpackages.com/packages/ericsnguyen-php-validation)
```

PHPackages © 2026

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