PHPackages                             spoorsny/laravel-model-validating-observer - 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. spoorsny/laravel-model-validating-observer

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

spoorsny/laravel-model-validating-observer
==========================================

An observer that validates an Eloquent model instance before it is persisted to the database.

v2.0.0(1y ago)010GPL-3.0-or-laterPHPPHP ^8.3

Since Aug 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/spoorsny/laravel-model-validating-observer)[ Packagist](https://packagist.org/packages/spoorsny/laravel-model-validating-observer)[ RSS](/packages/spoorsny-laravel-model-validating-observer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (9)Versions (3)Used By (0)

[![Repository Banner](https://camo.githubusercontent.com/960744f8a417659131330c4d383fa748d363d2154a9f0d73890ed86e8e524148/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4f62736572766572253230666f7225323076616c69646174696e67253230456c6f7175656e742532304d6f64656c2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d73706f6f72736e792532466c61726176656c2d6d6f64656c2d76616c69646174696e672d6f62736572766572267061747465726e3d63697263756974426f617264267374796c653d7374796c655f31266465736372697074696f6e3d416e2b6f627365727665722b746861742b76616c6964617465732b616e2b456c6f7175656e742b6d6f64656c2b696e7374616e63652b6265666f72652b69742b69732b7065727369737465642b746f2b7468652b64617461626173652e266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/960744f8a417659131330c4d383fa748d363d2154a9f0d73890ed86e8e524148/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4f62736572766572253230666f7225323076616c69646174696e67253230456c6f7175656e742532304d6f64656c2e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d73706f6f72736e792532466c61726176656c2d6d6f64656c2d76616c69646174696e672d6f62736572766572267061747465726e3d63697263756974426f617264267374796c653d7374796c655f31266465736372697074696f6e3d416e2b6f627365727665722b746861742b76616c6964617465732b616e2b456c6f7175656e742b6d6f64656c2b696e7374616e63652b6265666f72652b69742b69732b7065727369737465642b746f2b7468652b64617461626173652e266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

[![Latest Version on Packagist](https://camo.githubusercontent.com/18d7da014fd7ce3524263e6c8ab40b788f91f0b1a51c0031fd1456e8582fabdb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73706f6f72736e792f6c61726176656c2d6d6f64656c2d76616c69646174696e672d6f627365727665722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spoorsny/laravel-model-validating-observer)[![Total Downloads](https://camo.githubusercontent.com/0efaa74969029feb2985a0b808a2c032ce31e6aa6b960f0d2b0806b1188a4871/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73706f6f72736e792f6c61726176656c2d6d6f64656c2d76616c69646174696e672d6f627365727665722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spoorsny/laravel-model-validating-observer)[![GitHub Tests Action Status](https://camo.githubusercontent.com/7a2cc72d0201501f1a3cb08aeea1a2a86f79248a867d63d5b3e635ddc2837bc2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f73706f6f72736e792f6c61726176656c2d6d6f64656c2d76616c69646174696e672d6f627365727665722f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d6173746572266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/spoorsny/laravel-model-validating-observer/actions?query=workflow%3Acontinuous-integration+branch%3Amaster)[![PHPUnit Code Coverage](https://github.com/spoorsny/laravel-model-validating-observer/raw/image-data/coverage.svg)](https://github.com/spoorsny/laravel-model-validating-observer/actions?query=workflow%3Acontinuous-integration+branch%3Amaster)

Observer for validating Eloquent Model
======================================

[](#observer-for-validating-eloquent-model)

An observer that validates an Eloquent model instance before it is persisted to the database, by raising a `ValidationException`.

Install
-------

[](#install)

Use [Composer](https://getcomposer.org) to install the package.

```
composer require spoorsny/laravel-model-validating-observer
```

Usage
-----

[](#usage)

Add the `\Illuminate\Database\Eloquent\Attributes\ObservedBy` attribute to your model, with `\Spoorsny\Laravel\Observers\ValidateModel::class` as its argument.

Implement the `\Spoornsy\Laravel\Contracts\SelfValidatingModel` interface within your model by adding a public, static method to your model, named `validationRules()` that returns an associative array with the validation rules and custom messages for your model's attributes.

```
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Model;

use Spoorsny\Laravel\Contracts\SelfValidatingModel;
use Spoorsny\Laravel\Observers\ValidateModel;

#[ObservedBy(ValidateModel::class)]
class Car extends Model implements SelfValidatingModel
{
    public static function validationRules(): array
    {
        return [
            'rules' => [
                'make' => 'required|string',
                'model' => 'required|string',
            ],
            'messages' => [
                'make.required' => 'We need to know the make of the car.',
            ],
        ];
    }
}
```

The observer will check each instance of your model against the validation rules during the `saving` event triggered by Eloquent. If validation fails, an `\Illuminate\Validation\ValidationException` will be thrown, preventing the persistence of the invalid model instance.

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

[](#contributing)

To contribute to the package, see the [Contributing Guide](CONTRIBUTING.md).

License
-------

[](#license)

Copyright © 2024 Geoffrey Bernardo van Wyk

This file is part of package spoorsny/laravel-model-validating-observer.

Package spoorsny/laravel-model-validating-observer is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Package spoorsny/laravel-model-validating-observer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with package spoorsny/laravel-model-validating-observer. If not, see .

For a copy of the license, see the [LICENSE](LICENSE) file in this repository.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

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

Total

2

Last Release

642d ago

Major Versions

v1.0.0 → v2.0.02024-08-08

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f655900aa4d2538ee47a2c32177238d5d8c9bd2847fc91e7b08d26315f27a3f?d=identicon)[geoffreyvanwyk](/maintainers/geoffreyvanwyk)

---

Top Contributors

[![geoffreyvanwyk](https://avatars.githubusercontent.com/u/194185?v=4)](https://github.com/geoffreyvanwyk "geoffreyvanwyk (14 commits)")

---

Tags

eloquent-modelslaravellaravel-packageobserverobserver-design-patternobserver-patternpackagevalidationlaravelpackage

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/spoorsny-laravel-model-validating-observer/health.svg)

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

###  Alternatives

[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[olssonm/swedish-entity

Validator and formatter for Swedish personnummer and organisationsnummer. With Laravel validators.

15196.1k](/packages/olssonm-swedish-entity)[deligoez/tckimlikno

Turkish Identification Number Verification &amp; Validation Package for Laravel

2917.4k](/packages/deligoez-tckimlikno)[laravel-validation-rules/us-state

Validate US States and Canada Provinces

16172.7k](/packages/laravel-validation-rules-us-state)[larapack/command-validation

Enable a method for Artisan Commands to validate the output of methods like `ask`.

133.8k](/packages/larapack-command-validation)[janvince/smallcontactform

Simple but flexible multi language contact form builder with custom fields, validation and passive antispam

307.4k](/packages/janvince-smallcontactform)

PHPackages © 2026

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