minuteoflaravel / laravel-self-validating-model
Model class that auto-validates when model is saved or updated
Fund package maintenance!
minuteoflaravel
Installs: 1 954
Dependents: 0
Suggesters: 0
Security: 0
Stars: 42
Watchers: 2
Forks: 6
Open Issues: 1
Requires
- php: ^7.4|^8.0|^8.1
- illuminate/contracts: ^6.9|^7.0|^8.0|^9.0
- illuminate/database: ^6.9|^7.0|^8.0|^9.0
- illuminate/support: ^6.9|^7.0|^8.0|^9.0
README
This package contains an extended Eloquent Model class which self-validates against validation rules specified in the Model's rules property when the Model is saved/updated
Works with:
- Laravel 9
- Laravel 8
- Laravel 7
- Laravel 6
Installation
You can install package via composer:
composer require minuteoflaravel/laravel-self-validating-model
How to use
Let's say we have Contact model which extends Illuminate\Database\Eloquent\Model
:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Contact extends Model { use HasFactory; }
Instead of extending Illuminate\Database\Eloquent\Model
we should extend MinuteOfLaravel\Validation\SelfValidatingModel
:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use MinuteOfLaravel\Validation\SelfValidatingModel as Model; class Contact extends Model { use HasFactory; }
Then just add you validation rules to the $rules
property:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use MinuteOfLaravel\Validation\SelfValidatingModel as Model; class Contact extends Model { use HasFactory; public $rules = [ 'first_name' => 'required', 'last_name' => 'required', 'email' => 'required|email:rfc,dns,spoof', ]; }
That's it! Model will be validated on save/update.
Custom error messages
If you need to add your custom translatable error message then just add it as always to resources/lang/en/validation.php file.
License
The MIT License (MIT). Please see License File for more information.