PHPackages                             jasny/validation-result - 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. jasny/validation-result

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

jasny/validation-result
=======================

A result object for validation

v1.1.3(1y ago)4245.7k↓24.5%3[1 issues](https://github.com/jasny/validation-result/issues)5MITPHPPHP &gt;=8.1.0

Since Aug 10Pushed 1y agoCompare

[ Source](https://github.com/jasny/validation-result)[ Packagist](https://packagist.org/packages/jasny/validation-result)[ RSS](/packages/jasny-validation-result/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (8)Used By (5)

[![jasny-banner](https://user-images.githubusercontent.com/100821/62123924-4c501c80-b2c9-11e9-9677-2ebc21d9b713.png)](https://user-images.githubusercontent.com/100821/62123924-4c501c80-b2c9-11e9-9677-2ebc21d9b713.png)

Validation result
=================

[](#validation-result)

[![PHP](https://github.com/jasny/validation-result/actions/workflows/php.yml/badge.svg)](https://github.com/jasny/validation-result/actions/workflows/php.yml)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/15477c29d8a4a69caa0dff0764c6b1be73a914c201a073ab1b8198acde54c0bd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f76616c69646174696f6e2d726573756c742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/validation-result/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/6fe7c5809492a685ba8e9fdd410b31e77401d2bbf5f02e3634e70165201200ae/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f76616c69646174696f6e2d726573756c742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/validation-result/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/d3d6952149007cd116e4adcd00431cd1d3e39d20ad7f7dc6b5700e00018f0c80/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f76616c69646174696f6e2d726573756c742e737667)](https://packagist.org/packages/jasny/validation-result)[![Packagist License](https://camo.githubusercontent.com/070b0646c7886bc699bc24cfeb7a0ac4eb6303bcfff742325ac2272cec79c65e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f76616c69646174696f6e2d726573756c742e737667)](https://packagist.org/packages/jasny/validation-result)

A result object for a validation function.

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

[](#installation)

```
composer require jasny/validation-result

```

Examples
--------

[](#examples)

##### Validate variable

[](#validate-variable)

```
use Jasny\ValidationResult;

function validateVar($var)
{
    if (isset($var)) return ValidationResult::error("var isn't set");
    if ($var < 30) return ValidationResult::error("var is less than thirty");

    return ValidationResult::success();
}

$validation = validateVar($myVar);
if ($validation->failed()) echo $validation->getError();
```

##### Validate POST request

[](#validate-post-request)

```
use Jasny\ValidationResult;

function validateInput($input)
{
    $validation = new ValidationResult();

    if (!isset($input['baz'])) $validation->addError("baz isn't set");
    if (!isset($input['qux'])) $validation->addError("qux isn't set");

    return $validation;
}

$validation = validateInput($_POST);

if ($validation->succeeded()) {
    // Handle POST and redirect
    exit();
}

loadTemplate('myTemplate', ['errors' => $validation->getErrors()]);
```

Subvalidation
-------------

[](#subvalidation)

You can add the validation result of a subvalidation using the `add()` method. It's possible to prefix all the errors of the subvalidation.

```
use Jasny\ValidationResult;

function validateInput($input)
{
    $validation = new ValidationResult();

    if (!isset($input['baz'])) $validation->addError("baz isn't set");
    if (!isset($input['qux'])) $validation->addError("qux isn't set");

    if (isset($input['foo'])) {
        $fooValidation = validateFoo($input['foo']);
        $validation->add($fooValidation, 'foo');
    }

    return $validation;
}

function validateFoo($foo)
{
    $validation = new ValidationResult();

    if (empty($foo['name'])) $validation->addError("name isn't set");
    if (empty($foo['age'])) $validation->addError("age isn't set");

    return $validation;
}

$validation = validateInput($_POST);
```

Translation
-----------

[](#translation)

It's possible to translate the error messages using a callback.

```
use Jasny\ValidationResult;

$aliases = [
    "%s isn't set" => 'Please set %s',
    "%s is less than %d" => 'Please choose a value higher than %2$d for %1$s'
];

ValidationResult::$translate = function($message) use ($aliases) {
    return isset($aliases[$message]) ? $aliases[$message] : $message;
};

function validateVar($var)
{
    if (isset($var)) return ValidationResult::error("%s isn't set", 'Var');
    if ($var < 30) return ValidationResult::error("%s is less than %d", 'Var', 30);
}
```

or simply

```
ValidationResult::$translate = 'gettext';
```

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity77

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

Recently: every ~773 days

Total

7

Last Release

622d ago

Major Versions

v0.1.1 → v1.0.02016-03-14

PHP version history (3 changes)v0.1.0PHP &gt;=5.4.0

v1.0.0PHP &gt;=5.6.0

v1.1.3PHP &gt;=8.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (16 commits)")

---

Tags

validation

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jasny-validation-result/health.svg)

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

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k489.6M672](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k148.7M416](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

64736.9M186](/packages/opis-json-schema)[giggsey/libphonenumber-for-php-lite

A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber

8912.9M47](/packages/giggsey-libphonenumber-for-php-lite)

PHPackages © 2026

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