Trait to activate validation when saving Eloquent Model
, (*1)
This package provides a trait that will automatic handlind upload when saving/updating/deleting any Eloquent model with upload form request., (*2)
Requires
- php: >=7.0.0
- illuminate/database: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/validation: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
Installation
You can install the package via composer:
``` bash
$ composer require padosoft/laravel-validable, (*3)
## Usage
Your Eloquent models should use the `Padosoft\Laravel\Validable\Validable` trait.
You must define `protected static $rules` array of rules in your model.
You can define `protected static $messages` array of custom messages in your model.
Here's an example of how to implement the trait;
```php
<?php
namespace App;
use Padosoft\Laravel\Validable\Validable;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use Validable;
protected static $rules = [
'name'=>'required|max:10',
'order'=>'sometimes|integer|max:10',
];
protected static $messages = [
'name.required'=>'obbligatorio'
];
}
You can write specific validation for only update method, (*4)
class YourEloquentModel extends Model
{
use Validable;
protected static $rules = [
'name'=>'required|max:10|unique:table,field',
'order'=>'sometimes|integer|max:10',
];
protected static $updating_rules = [
'name'=>'required|max:10|unique:table,field,[id]',
'order'=>'sometimes|integer|max:10',
];
protected static $messages = [
'name.required'=>'obbligatorio'
];
}
Note: [id] will be overwritten at runtime with the model property., (*5)
You can check if your model is saved like this:, (*6)
$model = new YourEloquentModel;
$model->name='test';
if (!$model->save()){
$erros=$model->getErrors();
}
You can get a model validation rules:, (*7)
$rules=YourEloquentModel::getRules();
For all method available see the Validable Trait., (*8)
Change log
Please see CHANGELOG for more information what has changed recently., (*9)
Testing
bash
$ composer test
, (*10)
Contributing
Please see CONTRIBUTING for details., (*11)
Security
If you discover any security related issues, please email instead of using the issue tracker., (*12)
Credits
Inspired by https://github.com/JeffreyWay/Laravel-Model-Validation
- Lorenzo Padovani
- Leonardo Padovani
- All Contributors, (*13)
About Padosoft
Padosoft (https://www.padosoft.com) is a software house based in Florence, Italy. Specialized in E-commerce and web sites., (*14)
License
The MIT License (MIT). Please see License File for more information., (*15)