PHPackages                             usmanhalalit/dryval - 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. usmanhalalit/dryval

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

usmanhalalit/dryval
===================

Simplify validation in Laravel.

1.x-dev(11y ago)394.7k3MITPHPPHP &gt;=5.4.0

Since Apr 7Pushed 10y ago3 watchersCompare

[ Source](https://github.com/usmanhalalit/dryval)[ Packagist](https://packagist.org/packages/usmanhalalit/dryval)[ Docs](https://github.com/usmanhalalit/dryval)[ RSS](/packages/usmanhalalit-dryval/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (2)Versions (2)Used By (0)

DRYVal
======

[](#dryval)

A lightweight library to make your data validation easy, with Laravel PHP Framework.

Make your models self validating, just define the validation rules in your Eloquent model. Whenever you try to create or update data with your model and if validation fails, an exception will be thrown.

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

[](#installation)

Add this to require section (in your composer.json):

```
"usmanhalalit/dryval": "1.*@dev"

```

And run: `composer update`. Library on [Packagist](https://packagist.org/packages/usmanhalalit/dryval).

Usage Example
-------------

[](#usage-example)

```
use Dryval\ValidationTrait;

class Campaign extends Eloquent {
    use ValidationTrait;

    protected static $rules = [
        'title' => 'required|min:5|max:50', // `title` is the field in your table
        'description' => 'required|min:5|max:500',
    ];

}
```

Now every time a validation rule breaks a `Dryval\ValidationException` will be thrown.

### Laravel 5

[](#laravel-5)

To catch this put the code below in `render()` method of your `app/Exceptions/Handler.php`. It will look like this:

```
public function render($request, Exception $e)
{
    try {
        return \Redirect::back()->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
    } catch (Exception $exception) {
        // Fallback if referer doesn't exist
        return \Redirect::to('/')->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
    }
}
```

### Laravel 4

[](#laravel-4)

Put the code below in your `app/start/global.php` :

```
App::error(function(\Dryval\ValidationException $e)
{
    try {
        return \Redirect::back()->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
    } catch (Exception $exception) {
        // Fallback if referer doesn't exist
        return \Redirect::to('/')->with('error', $e->getMessages()->first())->withInput(\Input::except('password'));
    }
});
```

This will set a flush message on session with key `error`. You're free to do whatever you want. Simple as that.

Now you don't have to validate input everywhere. Just save the data and it will be automatically validated. DRYVal utilizes PHP 5.4 Traits to make this happen. So you are not forced to extend a class.

More on Usage
-------------

[](#more-on-usage)

### Custom Validation Messages

[](#custom-validation-messages)

If you want custom validation messages then you can put a `static $messages` array in your model. Example:

```
protected static $messages = [
     'unique' => 'This :attribute is already registered.'
];
```

### Base Model

[](#base-model)

If all of your Eloquent models extend a Base Model then you can `use` the trait in just your Base Model and you're good to go.

### Updating with Unique Rule

[](#updating-with-unique-rule)

When you validate using `unique` rule and you want skip certain id then you can just use `:id:` placeholder.

Let me clarify, you don't want to allow duplicate email addresses in your users table. You add this rule in your model:

```
protected static $rules = [
    'email' => 'required|email|unique:users,email'
];
```

Now a user signsup with  which is not already in database, that's fine. But now what if the user is trying to update his profile with some changes but same email address? Yes, validation will fail. To solve this problem Laravel accepts the 3rd param for unique rule. DRYVal makes it even easier just use `:id:` placeholder as 3rd param, its like a dynamic id. Example:

```
protected static $rules = [
    'email' => 'required|email|unique:users,email,:id:'
];
```

For this feature, credit goes to [Role Model](https://github.com/betawax/role-model).

### ValidationException

[](#validationexception)

DRYVal throws `Dryval\ValidationException` when validation fails. You can catch this exception app wise as shown in the first example or you can catch this in your controller/class. The `getMessages()`(plural) method of this exception class will return the validation error messages (MessageBag) same as Laravel's `$validator->messages()`. [More here](http://laravel.com/docs/validation#working-with-error-messages).

You can also throw this exception anywhere you need.

---

© 2015 [Muhammad Usman](http://usman.it/). Licensed under MIT license.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

4052d ago

### Community

Maintainers

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

---

Tags

laravelvalidationexceptionmodelself-validating

### Embed Badge

![Health badge](/badges/usmanhalalit-dryval/health.svg)

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

###  Alternatives

[propaganistas/laravel-phone

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

3.0k35.7M107](/packages/propaganistas-laravel-phone)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[wendelladriel/laravel-validated-dto

Data Transfer Objects with validation for Laravel applications

759569.4k13](/packages/wendelladriel-laravel-validated-dto)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[galahad/laravel-addressing

Laravel package providing addressing functionality

70316.6k](/packages/galahad-laravel-addressing)

PHPackages © 2026

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