This package provides a trait that will automatic handlind upload when saving/updating/deleting any Eloquent model with upload form request.
##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
You can install the package via composer:
$ composer require padosoft/laravel-validable
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
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
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.
You can check if your model is saved like this:
$model = new YourEloquentModel;
$model->name='test';
if (!$model->save()){
$erros=$model->getErrors();
}
You can get a model validation rules:
$rules=YourEloquentModel::getRules();
For all method available see the Validable Trait.
Please see CHANGELOG for more information what has changed recently.
$ composer test
Please see CONTRIBUTING for details.
If you discover any security related issues, please email instead of using the issue tracker.
Inspired by https://github.com/JeffreyWay/Laravel-Model-Validation
Padosoft (https://www.padosoft.com) is a software house based in Florence, Italy. Specialized in E-commerce and web sites.
The MIT License (MIT). Please see License File for more information.