https://github.com/padosoft/laravel-validable
Laravel trait to auto-validates a model on save.
https://github.com/padosoft/laravel-validable
laravel laravel-package model trait validation validator
Last synced: 6 months ago
JSON representation
Laravel trait to auto-validates a model on save.
- Host: GitHub
- URL: https://github.com/padosoft/laravel-validable
- Owner: padosoft
- License: mit
- Created: 2018-05-15T14:39:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2025-04-04T15:28:53.000Z (9 months ago)
- Last Synced: 2025-06-01T07:36:38.761Z (7 months ago)
- Topics: laravel, laravel-package, model, trait, validation, validator
- Language: PHP
- Size: 421 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Trait to activate validation when saving Eloquent Model
[](https://packagist.org/packages/padosoft/laravel-validable)
[](LICENSE.md)
[](https://circleci.com/gh/padosoft/laravel-validable/tree/master)
[](https://scrutinizer-ci.com/g/padosoft/laravel-validable)
[](https://packagist.org/packages/padosoft/laravel-validable)
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:
``` bash
$ 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
'required|max:10',
'order'=>'sometimes|integer|max:10',
];
protected static $messages = [
'name.required'=>'obbligatorio'
];
}
```
You can write specific validation for only update method
```php
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:
```php
$model = new YourEloquentModel;
$model->name='test';
if (!$model->save()){
$erros=$model->getErrors();
}
```
You can get a model validation rules:
```php
$rules=YourEloquentModel::getRules();
```
For all method available see the Validable Trait.
## Change log
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Testing
``` bash
$ composer test
```
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) 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
- [Lorenzo Padovani](https://github.com/lopadova)
- [Leonardo Padovani](https://github.com/leopado)
- [All Contributors](../../contributors)
## 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](LICENSE.md) for more information.