padosoft / laravel-validable
Trait to activate validation when saving Eloquent Model
Installs: 5 982
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 1
Open Issues: 1
Requires
- php: >=7.0.0
- illuminate/database: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/validation: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ~3.3.0|~3.4.2|^3.5.0|^4.0|^5.0|^6.0|^7.3|^8.0|^9.0
- phpunit/phpunit: ^5.7|6.2|^7.0|^8.0|^9.5.20|^10.0|^11.0
- roave/security-advisories: dev-latest
README
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
Installation
You can install the package via composer:
$ composer require padosoft/laravel-validable
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 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.
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email instead of using the issue tracker.
Credits
Inspired by https://github.com/JeffreyWay/Laravel-Model-Validation
About Padosoft
Padosoft (https://www.padosoft.com) is a software house based in Florence, Italy. Specialized in E-commerce and web sites.
License
The MIT License (MIT). Please see License File for more information.