PHPackages                             tobento/app-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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. tobento/app-validation

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

tobento/app-validation
======================

App validation support.

2.0(7mo ago)0107↓33.3%3MITPHPPHP &gt;=8.4

Since Jul 11Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/app-validation)[ Packagist](https://packagist.org/packages/tobento/app-validation)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-app-validation/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (4)Dependencies (10)Versions (6)Used By (3)

App Validation
==============

[](#app-validation)

Validation support for the app.

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

[](#table-of-contents)

- [Getting Started](#getting-started)
    - [Requirements](#requirements)
- [Documentation](#documentation)
    - [App](#app)
    - [Validator Boot](#validator-boot)
        - [Validate Data](#validate-data)
        - [Adding Rules](#adding-rules)
        - [Message Translation](#message-translation)
    - [Http Validation](#http-validation)
        - [Http Requirements](#http-requirements)
        - [Using Validation Request](#using-validation-request)
        - [Writing Request Validation](#using-validation-request)
        - [Http Validation Error Handler Boot](#http-validation-error-handler-boot)
    - [Live Validation](#live-validation)
- [Credits](#credits)

---

Getting Started
===============

[](#getting-started)

Add the latest version of the app validation project running this command.

```
composer require tobento/app-validation

```

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

[](#requirements)

- PHP 8.4 or greater

Documentation
=============

[](#documentation)

App
---

[](#app)

Check out the [**App Skeleton**](https://github.com/tobento-ch/app-skeleton) if you are using the skeleton.

You may also check out the [**App**](https://github.com/tobento-ch/app) to learn more about the app in general.

Validator Boot
--------------

[](#validator-boot)

The validator boot does the following:

- installs validator translation files
- validator and rules interfaces implementation

```
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();
```

### Validate Data

[](#validate-data)

You can validate data using the `ValidatorInterface::class`. You can access the validator in several ways:

Check out the [**Validation Service - Validating**](https://github.com/tobento-ch/service-validation/#validating) section learn more about validating data in general.

**Using the app**

```
use Tobento\App\AppFactory;
use Tobento\Service\Validation\ValidatorInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);
$app->booting();

$validator = $app->get(ValidatorInterface::class);

$validation = $validator->validating(
    value: 'foo',
    rules: 'alpha|minLen:2',
);

// var_dump($validation->isValid());
// bool(true)

// Run the app
$app->run();
```

**Using autowiring**

You can also request the `ValidatorInterface::class` in any class resolved by the app.

```
use Tobento\Service\Validation\ValidatorInterface;

class SomeService
{
    public function __construct(
        protected ValidatorInterface $validator,
    ) {}
}
```

### Adding Rules

[](#adding-rules)

The [**Default Rules**](https://github.com/tobento-ch/service-validation/#default-rules) are available by default. You may add more rules by the following way:

```
use Tobento\App\AppFactory;
use Tobento\Service\Validation\RulesInterface;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// using the app on method:
$app->on(RulesInterface::class, function(RulesInterface $rules) {
    $rules->add(name: 'same', rule: new Same());
});

// Run the app
$app->run();
```

You may check out the [**Rules**](https://github.com/tobento-ch/service-validation/#rules) section to learn more about adding rules.

### Message Translation

[](#message-translation)

Simply, install the [App Translation](https://github.com/tobento-ch/app-translation) bundle and boot the `\Tobento\App\Translation\Boot\Translation::class`:

```
composer require tobento/app-translation

```

```
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// Adding boots
$app->boot(\Tobento\App\Translation\Boot\Translation::class);
$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();
```

Messages will be translated based on the [Configured Translator Locale](https://github.com/tobento-ch/app-translation#configure-translator).

By default, the [Default Rules](https://github.com/tobento-ch/service-validation/#default-rules) error messages are translated in `en` and `de`.

Check out the [Add Translation](https://github.com/tobento-ch/app-translation#add-translations) section to learn how to add translations.

Make sure you define the resource name `validator` for rule error messages as configured on the [Message Translator Modifier](https://github.com/tobento-ch/service-message#translator). The [Message Parameter Translator Modifier](https://github.com/tobento-ch/service-message#parameter-translator) uses the `*` as resource name.

Http Validation
---------------

[](#http-validation)

#### Http Requirements

[](#http-requirements)

The following app example shows the minimum requirements for the http validation.

First, install the [App Http](https://github.com/tobento-ch/app-http) bundle:

```
composer require tobento/app-http

```

In addition, you may install the [App View](https://github.com/tobento-ch/app-view) bundle for view support:

```
composer require tobento/app-view

```

Next, make sure the following boots are defined:

```
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Add directories:
$app->dirs()
    ->dir(realpath(__DIR__.'/../'), 'root')
    ->dir(realpath(__DIR__.'/../app/'), 'app')
    ->dir($app->dir('app').'config', 'config', group: 'config')
    ->dir($app->dir('root').'public', 'public')
    ->dir($app->dir('root').'vendor', 'vendor');

// HTTP boots:
// Required for request validation:
$app->boot(\Tobento\App\Http\Boot\Routing::class);
$app->boot(\Tobento\App\Http\Boot\RequesterResponser::class);

// Required for flashing input and messages:
$app->boot(\Tobento\App\Http\Boot\Session::class);

// VIEW boots:
// Optional boots for view support:
$app->boot(\Tobento\App\View\Boot\View::class);
$app->boot(\Tobento\App\View\Boot\Form::class);
$app->boot(\Tobento\App\View\Boot\Messages::class);

// VALIDATION boots:
// Default error handler for handling ValidationException.
// You may create your own handler or add one with higher priority.
$app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class);

$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();
```

### Using Validation Request

[](#using-validation-request)

You may use the `Tobento\App\Validation\Http\ValidationRequest::class` for validating the request input.

By default, a `Tobento\App\Validation\Exception\ValidationException::class` will be thrown if validation fails. The exception will be handled by the `Tobento\App\Validation\Boot\HttpValidationErrorHandler::class` if booted. See [Http Validation Error Handler Boot](#http-validation-error-handler-boot).

```
use Tobento\App\Validation\Http\ValidationRequest;
use Tobento\Service\Validation\ValidationInterface;
use Tobento\Service\Requester\RequesterInterface;
use Tobento\Service\Responser\ResponserInterface;
use Tobento\Service\Routing\RouterInterface;
use Psr\Http\Message\ResponseInterface;

class ProductController
{
    /**
     * Store a new product.
     */
    public function store(ValidationRequest $request): ResponseInterface
    {
        $validation = $request->validate(
            rules: [
                'title' => 'required|alpha',
            ],

            // You may specify an uri for redirection.
            redirectUri: '/products/create',

            // Or you may specify a route name for redirection.
            redirectRouteName: 'products.create',

            // If no uri or route name is specified,
            // it will be redirected to the previous url.

            // You may specify an error message flashed to the user.
            errorMessage: 'You have some errors check out the fields for its error message',

            // You may change the behaviour by a custom validation error handler though.
        );

        // The product is valid, store product e.g.

        var_dump($validation instanceof ValidationInterface);
        // bool(true)

        // The following interfaces are available:
        var_dump($request->requester() instanceof RequesterInterface);
        // bool(true)

        var_dump($request->responser() instanceof ResponserInterface);
        // bool(true)

        var_dump($request->router() instanceof RouterInterface);
        // bool(true)

        // you may use the responser and router for redirection:
        return $request->responser()->redirect($request->router()->url('products.index'));
    }
}
```

You may check out the following links for its documentation:

- [Validation Interface](https://github.com/tobento-ch/service-validation/#validation)
- [Requester](https://github.com/tobento-ch/service-requester#documentation)
- [Responser](https://github.com/tobento-ch/service-responser#responser)
- [Router](https://github.com/tobento-ch/service-routing)

**Manually handling validation**

```
use Tobento\App\Validation\Http\ValidationRequest;
use Psr\Http\Message\ResponseInterface;

class ProductController
{
    public function store(ValidationRequest $request): ResponseInterface
    {
        $validation = $request->validate(
            rules: [
                'title' => 'required|alpha',
            ],

            // you may disable throwing ValidationException on failure
            // and handling it by yourself.
            throwExceptionOnFailure: false,
        );

        if (! $validation->isValid()) {
            // handle invalid validation.
        }

        // ...
    }
}
```

### Http Validation Error Handler Boot

[](#http-validation-error-handler-boot)

The http error handler boot does the following:

- handles `Tobento\App\Validation\Exception\ValidationException::class` exceptions.

```
use Tobento\App\AppFactory;

// Create the app
$app = new AppFactory()->createApp();

// Adding boots
$app->boot(\Tobento\App\Validation\Boot\HttpValidationErrorHandler::class);

$app->boot(\Tobento\App\Validation\Boot\Validator::class);

// Run the app
$app->run();
```

When the incoming HTTP request is expecting a JSON response, the error handler will return a 422 Unprocessable Entity HTTP response with the following format:

```
{
    "message": "The exception message",
    "errors": {
        "email": [
            "The email is required."
        ]
    }
}
```

Otherwise, the error handler will return a redirect response by using the defined `redirectUri` parameter from the `Tobento\App\Validation\Exception\ValidationException::class` or if not defined using the `Tobento\Service\Uri\PreviousUriInterface::class` uri:

Furthermore, you may create a custom [Error Handler](https://github.com/tobento-ch/app-http/#handle-other-exceptions) or add an [Error Handler With A Higher Priority](https://github.com/tobento-ch/app-http/#prioritize-error-handler) of `3000` as defined on the `Tobento\App\Validation\Boot\HttpValidationErrorHandler::class`.

Live Validation
---------------

[](#live-validation)

In progress...

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance63

Regular maintenance activity

Popularity12

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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 ~163 days

Recently: every ~147 days

Total

6

Last Release

227d ago

Major Versions

1.x-dev → 2.02025-10-02

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (6 commits)")

---

Tags

validatorvalidationpackageapptobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-app-validation/health.svg)

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

PHPackages © 2026

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