PHPackages                             tag/sudzy - 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. tag/sudzy

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

tag/sudzy
=========

Model validator for use with Paris (and Idiorm).

v0.3.0(9y ago)126676[1 PRs](https://github.com/tag/sudzy/pulls)BSDPHPPHP &gt;=5.6.0

Since Oct 14Pushed 8y ago4 watchersCompare

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

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

Sudzy [![Build Status](https://camo.githubusercontent.com/7c27558b8f57af5696e9e9c2835274cff96dfdc5ae6119026e71c7b2ed607461/68747470733a2f2f7472617669732d63692e6f72672f7461672f7375647a792e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/tag/sudzy)
================================================================================================================================================================================================================================================================

[](#sudzy-)

**Breaking change on v0.3: Validations now use `respect/validation` validation library.**

Sudzy implements validarion against model classes using [Paris](https://github.com/j4mie/paris)/[Idiorm](https://github.com/j4mie/idiorm) (an active record ORM, often used with Slim), although it could be adapted easily.

Sudzy's `ValidModel` class decorates Paris' `Model` class. By extending `ValidModel`, your model classes gain immediate access to validations.

By default the `ValidModel` will store validation errors when model properties are set (for an exising model) or a new model is saved, and throw a `ValidationException` on save if errors were encountered.

Sudzy's `ValidModel` class uses [`Respect/Validation`](https://github.com/Respect/Validation) as its validation engine. See that project for details.

### Installation

[](#installation)

The easiest way to install Sudzy is via [Composer](http://getcomposer.org). Start by creating or adding to your project's `composer.json` file:

```
    {
        "require": {
            "tag/sudzy" : "dev-master" // Grab the most recent version from github
        }
    }
```

ValidModel Example
------------------

[](#validmodel-example)

The `ValidModel` class requires you to implement the abstract method `#prepareValidations()`, in order to lazily load the validations. Thus, constructors will not have the overhead of creating unused validation objects.

Validations can also be added at any time with the `#addValidation()` method.

The `#setValidation()` method is passed the model property to watch, and a Respect validation object to be checked against. Multiple calls on the same property overwrite previous validations.

`Respect\Validation` is namespaced, but you can make your life easier by importing a single class into your context:

```
    use Respect\Validation\Validator as v;
```

```
    // Within a `ValidModel` class declaration:

    public function prepareValidation()
    {
        $this->setValidation('username', v::alnum()->noWhitespace()->length(1, 15) );
        $this->setValidation('email', v::email() );
        $this->setValidation('password', v::stringType()->length(6, null)->length(1, 15) );
        $this->setValidation('birthdate', v::date()->age(18));

    }
```

When using `Respect\Validation`, create different validations for each field, instead of a single validator for the entire object.

### Full Example

[](#full-example)

Example model class:

```
namespace Models;

use Respect\Validation\Validator as v;

class User extends \Sudzy\ValidModel
{
    public function prepareValidation()
    {
        $this->setValidation('username', v::alnum()->noWhitespace()->length(1, 15) );
        $this->setValidation('email', v::email() );
        $this->setValidation('password', v::stringType()->length(6, null)->length(1, 15) );
        $this->setValidation('birthdate', v::date()->age(18));
    }
}
```

Example controller snip:

```
   // This example assumes Slim context and access to flash messages
   // ...  ...

   $newUser = Model::factory('\Models\User')->create();

   try {
       $newUser->email = $_POST['email'];
       $newUser->password = $_POST['password'];

       $newWard->save();

       $this->flash->addMessage('success', 'New User created.');
   } catch (Sudzy\ValidationException $sve) {
       foreach ($sve->getMessages() as $msg) {
           $this->flash->addMessage('error', $msg);
       }
   }
```

### Validation Exceptions and Errors

[](#validation-exceptions-and-errors)

By default, Sudzy's `ValidModel` does validation checks whenever objects are committed to the database via `#save()`, but can be configured to throw an exception when properties are set, or not at all.

Because an object can have multiple fields fail, it is necessary to catch and wrap Respect's exceptions.

:TODO:

Validation failures are stored, and available through `getValidationErrors()`, a method of both the `ValidModel` object and the thrown `ValidationException`. An object that fails validation throws a `ValidationException` when `save()` is attempted (default behavior). This can be changed to `::ON_SET` or `::NEVER` by setting the `throw` option:

```
$model->setValidationOptions(
    array('throw' => self::ON_SET)
);
```

Be careful of using `::ON_SET`, as Paris' internal `set()` method is not called when a model is built via Paris' `hydrate()` or `create()` methods. Also, `::ON_SET` tiggers the validation exception immediately, whereas `::ON_SAVE`permits validating all fields before throwing an exception.

Regardless of the value of the `throw` option, validations are checked when properties are set. In the case of new models (such as one built with Paris methods `create()` or `hydrate()`), validations are also checked on save. Regardless of when exceptions are thrown (or not), errors are immediately available through `getValidationErrors()`.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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 ~415 days

Total

4

Last Release

3351d ago

PHP version history (2 changes)v0.1PHP &gt;=5.3.0

v0.3.0PHP &gt;=5.6.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/5632fe2d93e27ebe3d99e6c0278fbdf4a945f9fc93f69df99a1053ffab3a20ee?d=identicon)[tag](/maintainers/tag)

---

Top Contributors

[![tag](https://avatars.githubusercontent.com/u/9618?v=4)](https://github.com/tag "tag (12 commits)")[![Patabugen](https://avatars.githubusercontent.com/u/196795?v=4)](https://github.com/Patabugen "Patabugen (5 commits)")[![DavidePastore](https://avatars.githubusercontent.com/u/1949364?v=4)](https://github.com/DavidePastore "DavidePastore (1 commits)")

---

Tags

validatormodelidiormparis

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/tag-sudzy/health.svg)

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

###  Alternatives

[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[seld/jsonlint

JSON Linter

1.3k217.8M205](/packages/seld-jsonlint)[composer/spdx-licenses

SPDX licenses list and validation library.

1.4k184.2M25](/packages/composer-spdx-licenses)[opis/json-schema

Json Schema Validator for PHP

64236.9M186](/packages/opis-json-schema)[laminas/laminas-validator

Validation classes for a wide range of domains, and the ability to chain validators to create complex validation criteria

15544.9M188](/packages/laminas-laminas-validator)[ergebnis/json-schema-validator

Provides a JSON schema validator, building on top of justinrainbow/json-schema.

3626.9M7](/packages/ergebnis-json-schema-validator)

PHPackages © 2026

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