jaspaul/eloquent-model-validation

A simple package that provides a venue for model validation in Laravel.
20,415 9
Install
composer require jaspaul/eloquent-model-validation
Latest Version:1.0
PHP:>=7.2
License:MIT
Last Updated:May 27, 2024
Links: GitHub  ·  Packagist
Maintainer: Jaspaul

Eloquent Model Validation

Build
Status Coverage
Status Code Climate

This package is an experiment in introducing the concept of active record validations to Laravel.

Install

Via Composer

$ composer require jaspaul/eloquent-model-validation

Requirements

The following versions of PHP are supported by this version.

  • PHP 7.2
  • PHP 7.3
  • PHP 7.4

Configuration

Option 1

Extend the validation model instead of Eloquent\Model.

<?php

use Jaspaul\EloquentModelValidation\Model;

class Foo extends Model
{
    ...
}
Option 2

Override Eloquent in config/app.php

'Eloquent' => Jaspaul\EloquentModelValidation\Model::class,

Now you can just do the following:

<?php

class Foo extends Eloquent
{
    ...
}
Option 3

Update your classes to implement Validatable using the Validates trait provided. You can use this option if extending the provided model isn't an option in your project.

<?php

namespace Jaspaul\EloquentModelValidation;

use Illuminate\Database\Eloquent\Model as Base;
use Jaspaul\EloquentModelValidation\Traits\Validates;
use Jaspaul\EloquentModelValidation\Contracts\Validatable;

abstract class Model extends Base implements Validatable
{
    use Validates;

    /**
     * Returns the data to validate.
     *
     * @return array
     */
    protected function getData() : array
    {
        return $this->getAttributes();
    }
}

Usage

<?php

use Jaspaul\EloquentModelValidation\Model;

class User extends Model
{
    protected function getRules() : array
    {
        return [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ];
    }
}

Now if you were storing your model:

public function store()
{
    $user = new User(Input::all());

    try {
        $user->save();
        return $user;
    } catch (\Illuminate\Validation\ValidationException $exception) {
        // You can handle exception, access the errors $exception->getErrors(),
        // or let it bubble up and let the Laravel Exception handler deal with it.

        throw $exception;
    }
}

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

641,106,524 9,700
laravel/framework

The Laravel Framework.

593,882,149 34,959
laravel/tinker

Powerful REPL for the Laravel framework.

502,645,077 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

418,291,778 609
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

394,964,158 4,660

Version History

Version Released PHP Laravel License
1.0 >=7.2 ^6.0 MIT
v0.1.3 >=7.0 ^5.4 MIT
v0.1.2 >=7.0 ^5.4 MIT
v0.1.1 >=7.0 ^5.4 MIT
v0.1 >=7.0 ^5.4 MIT