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

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

ksraylan/laravel-model-validation
=================================

Model validation for Laravel application.

07PHP

Since Apr 22Pushed 1y agoCompare

[ Source](https://github.com/ksraylan/laravel-model-validation)[ Packagist](https://packagist.org/packages/ksraylan/laravel-model-validation)[ RSS](/packages/ksraylan-laravel-model-validation/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

`♥ Made with  And I love `

[![](https://camo.githubusercontent.com/9755c0ffed17a24ecdf1205f06baf32fd5acfaf7a998d6ebe783b348ebb600f6/68747470733a2f2f6170702e666f7373612e636f6d2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d736869656c64)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield)

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

[](#laravel-model-validation)

[![FOSSA Status](https://camo.githubusercontent.com/ad7b2d5a6038706bfed573f2fb6cd421f949839fd050bc5ea1d068bb4c847067/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d736869656c64)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_shield)

Model validation - Validates the model data. \*Only for laravel applications.

An easy validator option for your eloquent models. Also have flexibility for additional codes that might be executed on before and after validation.

### Composer install

[](#composer-install)

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

Usage Examples
--------------

[](#usage-examples)

Here user model is mentioned as an example. You could use this in any model you want.

### User.php model

[](#userphp-model)

```
use Enigma\ValidatorTrait;

class User extends Model
{
    use ValidatorTrait;

    /**
     * Boot method.
     */
    public static function boot()
    {
        parent::boot();

        // Add this method for validating the current model on model saving event
        static::validateOnSaving();
    }

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

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

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

    /**
     * Code to be executed before the validation goes here.
     */
    public function beforeValidation()
    {
        // Some code goes here..
    }

    /**
     * Code to be executed after the validation goes here.
     */
    public function afterValidation()
    {
        // Some code goes here..
    }
}
```

### Control the data get validated

[](#control-the-data-get-validated)

You can control the data which gets validated by adding validationData method.

```
/**
 * Validation data to be validated.
 *
 * @return array
 */
public function validationData(array $data)
{
    // Here $data is the value of $this->getAttributes(), feel free to use your own code to produce the data. Ex: $this->toArray(), $this->getOriginal(), etc.,
    $data["name"] = strtolower($data["name"]);

    // Note: This wouldn't affect your actual model data which is going to persist in DB.

    return $data;
}
```

### Other options

[](#other-options)

You could mention the validation rules, attributes and messages as a property as well as method.

```
/**
 * Validation rules to validate.
 *
 * @return array
 */
public function validationRules()
{
    // You can process your code here and return the rules as however you want.
    return [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];
}

/**
 * Custom messages to replace the validation messages.
 *
 * @return array
 */
public function validationMessages()
{
    // You can process your code here and return the messages as however you want.
    return [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];
}

/**
 * Custom attribute names to replace the validation attribute name.
 *
 * @return array
 */
public function validationAttributes()
{
    return [
        'name' => 'User Name'
    ];
}
```

You could mention the validation only for creating itself or on any model event just add `$model->validate()`.

```
/**
 * Boot method.
 */
public static function boot()
{
    parent::boot();

    // You can mention like this for validating the model on custom events as your wish
    self::creating(function($model){
        $model->validate();
    });

    // Or you can make use of the alias `self::validateOnCreating()`.
}
```

Refer the available methods in the ValidationTrait.

License
-------

[](#license)

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

[![FOSSA Status](https://camo.githubusercontent.com/c084773db5b14f657c72d8f6d7979174226321d0c8596c0274a32d6da2524c14/68747470733a2f2f6170702e666f7373612e696f2f6170692f70726f6a656374732f6769742532426769746875622e636f6d253246746865726964646c656f66656e69676d612532466c61726176656c2d6d6f64656c2d76616c69646174696f6e2e7376673f747970653d6c61726765)](https://app.fossa.io/projects/git%2Bgithub.com%2Ftheriddleofenigma%2Flaravel-model-validation?ref=badge_large)

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 85.1% 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.

### Community

Maintainers

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

---

Top Contributors

[![theriddleofenigma](https://avatars.githubusercontent.com/u/29883026?v=4)](https://github.com/theriddleofenigma "theriddleofenigma (63 commits)")[![kumaravel011](https://avatars.githubusercontent.com/u/30690409?v=4)](https://github.com/kumaravel011 "kumaravel011 (6 commits)")[![ksraylan](https://avatars.githubusercontent.com/u/32464286?v=4)](https://github.com/ksraylan "ksraylan (2 commits)")[![fossabot](https://avatars.githubusercontent.com/u/29791463?v=4)](https://github.com/fossabot "fossabot (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)")

### Embed Badge

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

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

###  Alternatives

[marcosh/php-validation-dsl

A DSL for validating data in a functional fashion

483.9k](/packages/marcosh-php-validation-dsl)

PHPackages © 2026

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