2017 © Pedro Peláez
 

library eloquent-model-validation

A simple package that provides a venue for model validation in Laravel.

image

jaspaul/eloquent-model-validation

A simple package that provides a venue for model validation in Laravel.

  • Tuesday, August 29, 2017
  • by Jaspaul
  • Repository
  • 1 Watchers
  • 7 Stars
  • 10,601 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 12 % Grown

The README.md

Eloquent Model Validation

Build
Status Coverage
Status Code Climate, (*1)

This package is an experiment in introducing the concept of active record validations to Laravel., (*2)

Install

Via Composer, (*3)

``` bash $ composer require jaspaul/eloquent-model-validation, (*4)


## 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 <?php use Jaspaul\EloquentModelValidation\Model; class Foo extends Model { ... }

Option 2

Override Eloquent in config/app.php, (*5)

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

Now you can just do the following:, (*6)

<?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., (*7)

<?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:, (*8)

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;
    }
}

The Versions

29/08 2017

dev-master

9999999-dev

A simple package that provides a venue for model validation in Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/08 2017

v0.1.3

0.1.3.0

A simple package that provides a venue for model validation in Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

29/08 2017

v0.1.2

0.1.2.0

A simple package that provides a venue for model validation in Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

26/04 2017

v0.1.1

0.1.1.0

A simple package that provides a venue for model validation in Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

14/03 2017

v0.1

0.1.0.0

A simple package that provides a venue for model validation in Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires