An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# Trait to activate validation when saving Eloquent Model

[![Latest Version on Packagist](https://img.shields.io/packagist/v/padosoft/laravel-validable.svg?style=flat-square)](https://packagist.org/packages/padosoft/laravel-validable)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![CircleCI](https://circleci.com/gh/padosoft/laravel-validable/tree/master.svg?style=svg)](https://circleci.com/gh/padosoft/laravel-validable/tree/master)
[![Quality Score](https://img.shields.io/scrutinizer/g/padosoft/laravel-validable.svg?style=flat-square)](https://scrutinizer-ci.com/g/padosoft/laravel-validable)
[![Total Downloads](https://img.shields.io/packagist/dt/padosoft/laravel-validable.svg?style=flat-square)](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.