PHPackages                             theriddleofenigma/laravel-model-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. [Database &amp; ORM](/categories/database)
4. /
5. theriddleofenigma/laravel-model-validation

ActiveLibrary[Database &amp; ORM](/categories/database)

theriddleofenigma/laravel-model-validation
==========================================

Effortless, self-contained validation for your Eloquent models.

v2.0.0(3w ago)3511.0k↓58.4%5[1 issues](https://github.com/theriddleofenigma/laravel-model-validation/issues)9MITPHPPHP ^8.2CI failing

Since Nov 17Pushed 1y ago2 watchersCompare

[ Source](https://github.com/theriddleofenigma/laravel-model-validation)[ Packagist](https://packagist.org/packages/theriddleofenigma/laravel-model-validation)[ Docs](https://github.com/theriddleofenigma/laravel-model-validation)[ RSS](/packages/theriddleofenigma-laravel-model-validation/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (8)Dependencies (8)Versions (11)Used By (9)

Laravel Model Validation
========================

[](#laravel-model-validation)

[![Tests](https://github.com/theriddleofenigma/laravel-model-validation/actions/workflows/tests.yml/badge.svg)](https://github.com/theriddleofenigma/laravel-model-validation/actions/workflows/tests.yml)[![Latest Stable Version](https://camo.githubusercontent.com/2be826c76199bb8faf5a2903db0da5c0894b69235f869710ff43dc122bae1243/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746865726964646c656f66656e69676d612f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2e737667)](https://packagist.org/packages/theriddleofenigma/laravel-model-validation)[![Total Downloads](https://camo.githubusercontent.com/a8ea73f5585fcb63fc12aa1d3c383255070e907b0d39275d36e252fd8bf43c6c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f746865726964646c656f66656e69676d612f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2e737667)](https://packagist.org/packages/theriddleofenigma/laravel-model-validation)[![License](https://camo.githubusercontent.com/c95622f18bcc96eda027df2198440c89f3b26aa9226717de3aa3c05660961350/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f746865726964646c656f66656e69676d612f6c61726176656c2d6d6f64656c2d76616c69646174696f6e2e737667)](https://packagist.org/packages/theriddleofenigma/laravel-model-validation)

Effortless, self-contained validation for your Eloquent models.

Keep your validation rules where the data lives. Declare the rules on the model, opt in to the model event you care about, and every save is validated automatically — no form requests, no repeated calls to the validator.

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

[](#requirements)

PackageVersionPHP8.2, 8.3, 8.4Laravel12.x, 13.x> Laravel 13 requires PHP 8.3 or newer. Older Laravel releases that have reached end-of-life are not supported.

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

[](#installation)

```
composer require theriddleofenigma/laravel-model-validation
```

Quick start
-----------

[](#quick-start)

Add the `Enigma\ValidatorTrait` to a model, declare its rules, and register the event you want to validate on:

```
use Enigma\ValidatorTrait;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use ValidatorTrait;

    public array $validationRules = [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];

    protected static function boot(): void
    {
        parent::boot();

        // Validate the model automatically whenever it is saved.
        static::validateOnSaving();
    }
}
```

Now any attempt to save an invalid model throws an `Illuminate\Validation\ValidationException`, exactly like Laravel's own validation — so in an HTTP context the errors are flashed and redirected for you automatically.

```
User::create(['name' => 'Kumar', 'email' => 'not-an-email']); // throws ValidationException
```

Registering validation
----------------------

[](#registering-validation)

Three helpers register the matching Eloquent event listener for you:

```
static::validateOnSaving();   // fires on create and update
static::validateOnCreating(); // fires on create only
static::validateOnUpdating(); // fires on update only
```

Prefer to validate on a different event, or on demand? Call `validate()`yourself. It returns the validated data and throws on failure:

```
$validated = $user->validate();
```

Customising the configuration
-----------------------------

[](#customising-the-configuration)

Rules, messages and attribute names can each be declared **either** as a property **or** as a method of the same name. A method always takes precedence, so you can compute the configuration dynamically when you need to.

```
class User extends Model
{
    use ValidatorTrait;

    public array $validationMessages = [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in an invalid format.',
    ];

    public array $validationAttributes = [
        'name' => 'User Name',
    ];

    public function validationRules(): array
    {
        return [
            'name' => 'required|max:10',
            'email' => ['required', 'email', 'unique:users,email,' . $this->id],
        ];
    }
}
```

Controlling the data that gets validated
----------------------------------------

[](#controlling-the-data-that-gets-validated)

By default the model's raw attributes are validated. Declare a `validationData()` method to reshape that data first — the returned value is used only for validation and never changes what is persisted.

```
/**
 * @param  array  $data  The value of $this->getAttributes().
 * @return array
 */
public function validationData(array $data): array
{
    $data['name'] = strtolower($data['name']);

    return $data;
}
```

Skipping validation
-------------------

[](#skipping-validation)

Sometimes you need to persist a model without validating it — a seeder, a data import, or an admin override. There are three ways to do it.

Skip it on a single instance and save:

```
$user = new User(['name' => 'Kumar']);

$user->skipValidation()->save();
// or, for a one-off save that leaves the instance's state untouched:
$user->saveWithoutValidation();
```

Toggle the flag back on when you need to:

```
$user->skipValidation();       // subsequent saves are not validated
$user->skipValidation(false);  // validation is back on
```

Disable validation for a whole block — the cleanest way to skip it when creating through the query builder:

```
User::withoutValidation(function () {
    User::create(['name' => 'Kumar']); // not validated
});

// the return value of the callback is passed through
$user = User::withoutValidation(fn () => User::create(['name' => 'Kumar']));
```

Validation is automatically re-enabled once the callback finishes, even if it throws.

Before &amp; after hooks
------------------------

[](#before--after-hooks)

Implement `beforeValidation()` and/or `afterValidation()` to run logic around each validation pass:

```
public function beforeValidation(): void
{
    // Normalise attributes, set defaults, etc.
}

public function afterValidation(): void
{
    // Anything that should run once validation succeeds.
}
```

Testing
-------

[](#testing)

```
composer install
composer test
```

Contributing
------------

[](#contributing)

Pull requests are welcome! Please read the [contributing guide](CONTRIBUTING.md)first, make sure the test suite passes, and add coverage for any behaviour you change. Notable changes are tracked in the [changelog](CHANGELOG.md).

Security
--------

[](#security)

If you discover a security vulnerability, please follow the process in [SECURITY.md](SECURITY.md) rather than opening a public issue.

Code of Conduct
---------------

[](#code-of-conduct)

This project follows a [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold it.

License
-------

[](#license)

Laravel Model Validation is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance65

Regular maintenance activity

Popularity37

Limited adoption so far

Community26

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 86.5% 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 ~311 days

Recently: every ~260 days

Total

10

Last Release

26d ago

Major Versions

0.1.7 → v1.02020-10-27

v1.6.0 → v2.0.02026-07-24

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/29883026?v=4)[Kumaravel](/maintainers/theriddleofenigma)[@theriddleofenigma](https://github.com/theriddleofenigma)

---

Top Contributors

[![theriddleofenigma](https://avatars.githubusercontent.com/u/29883026?v=4)](https://github.com/theriddleofenigma "theriddleofenigma (64 commits)")[![kumaravel011](https://avatars.githubusercontent.com/u/30690409?v=4)](https://github.com/kumaravel011 "kumaravel011 (6 commits)")[![fossabot](https://avatars.githubusercontent.com/u/29791463?v=4)](https://github.com/fossabot "fossabot (1 commits)")[![ksraylan](https://avatars.githubusercontent.com/u/32464286?v=4)](https://github.com/ksraylan "ksraylan (1 commits)")[![rubenmuehlhans](https://avatars.githubusercontent.com/u/85454114?v=4)](https://github.com/rubenmuehlhans "rubenmuehlhans (1 commits)")[![syehan](https://avatars.githubusercontent.com/u/21031766?v=4)](https://github.com/syehan "syehan (1 commits)")

---

Tags

databaseeloquenteloquent-modelslaravellaravel-applicationlaravel-frameworkmodelmodel-validationphpvalidationlaravelvalidatorvalidationmodeleloquent

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/theriddleofenigma-laravel-model-validation/health.svg)

```
[![Health](https://phpackages.com/badges/theriddleofenigma-laravel-model-validation/health.svg)](https://phpackages.com/packages/theriddleofenigma-laravel-model-validation)
```

###  Alternatives

[api-platform/laravel

API Platform support for Laravel

58190.1k21](/packages/api-platform-laravel)[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8723.3M27](/packages/yajra-laravel-oci8)[propaganistas/laravel-disposable-email

Disposable email validator

6023.2M7](/packages/propaganistas-laravel-disposable-email)[laravel/ai

The official AI SDK for Laravel.

1.1k4.6M322](/packages/laravel-ai)[watson/validating

Eloquent model validating trait.

9733.6M55](/packages/watson-validating)

PHPackages © 2026

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