Looking to hire Laravel developers? Try LaraJobs

laravel-validable maintained by padosoft

Description
Trait to activate validation when saving Eloquent Model
Last update
2025/08/07 11:52 (dev-junie-init)
License
Downloads
13 033

Comments
comments powered by Disqus

Trait to activate validation when saving Eloquent Model

Latest Version on Packagist Software License CircleCI Quality Score Total Downloads

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.