2017 © Pedro Peláez
 

library laravel-model-validation

Model validation for Laravel application.

image

codedevpal/laravel-model-validation

Model validation for Laravel application.

  • Tuesday, July 24, 2018
  • by theriddleofenigma
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

♥ Made with <love/> And I love <code/>, (*1)

, (*2)

Laravel Model Validation

FOSSA Status, (*3)

Model validation - Validates the model data. *Only for laravel applications., (*4)

An easy validator option for your eloquent models. Also have flexibility for additional codes that might be executed on before and after validation., (*5)

Composer install

composer require theriddleofenigma/laravel-model-validation

Usage Examples

Here user model is mentioned as an example. You could use this in any model you want., (*6)

User.php model

use Enigma\ValidatorTrait;

class User extends Model
{
    use ValidatorTrait;

    /**
     * Boot method.
     */
    public static function boot()
    {
        parent::boot();

        // Add this method for validating the current model on model saving event
        static::validateOnSaving();
    }

    public $validationRules = [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];

    public $validationMessages = [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];

    public $validationAttributes = [
        'name' => 'User Name'
    ];

    /**
     * Code to be executed before the validation goes here.
     */
    public function beforeValidation()
    {
        // Some code goes here..
    }

    /**
     * Code to be executed after the validation goes here.
     */
    public function afterValidation()
    {
        // Some code goes here..
    }
}

Control the data get validated

You can control the data which gets validated by adding validationData method., (*7)

/**
 * Validation data to be validated.
 *
 * @return array
 */
public function validationData(array $data)
{
    // Here $data is the value of $this->getAttributes(), feel free to use your own code to produce the data. Ex: $this->toArray(), $this->getOriginal(), etc.,
    $data["name"] = strtolower($data["name"]);

    // Note: This wouldn't affect your actual model data which is going to persist in DB.

    return $data;
}

Other options

You could mention the validation rules, attributes and messages as a property as well as method., (*8)

/**
 * Validation rules to validate.
 *
 * @return array
 */
public function validationRules()
{
    // You can process your code here and return the rules as however you want.
    return [
        'name' => 'required|max:10',
        'email' => 'required|email',
    ];
}

/**
 * Custom messages to replace the validation messages.
 *
 * @return array
 */
public function validationMessages()
{
    // You can process your code here and return the messages as however you want.
    return [
        'name.required' => 'Name field is required.',
        'email.email' => 'The given email is in invalid format.',
    ];
}

/**
 * Custom attribute names to replace the validation attribute name.
 *
 * @return array
 */
public function validationAttributes()
{
    return [
        'name' => 'User Name'
    ];
}

You could mention the validation only for creating itself or on any model event just add $model->validate()., (*9)

/**
 * Boot method.
 */
public static function boot()
{
    parent::boot();

    // You can mention like this for validating the model on custom events as your wish
    self::creating(function($model){
        $model->validate();
    });

    // Or you can make use of the alias `self::validateOnCreating()`.
}

Refer the available methods in the ValidationTrait., (*10)

License

Laravel Model Validation is open-sourced software licensed under the MIT license., (*11)

FOSSA Status, (*12)

The Versions

24/07 2018

dev-master

9999999-dev

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

24/07 2018

0.1.6

0.1.6.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

24/07 2018

0.1.5

0.1.5.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

24/07 2018

0.1.4

0.1.4.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

21/07 2018

0.1.2

0.1.2.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

21/07 2018

0.1.3

0.1.3.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

21/07 2018

0.1.1

0.1.1.0

Model validation for Laravel application.

  Sources   Download

The Requires

 

by Kumaravel

21/07 2018

0.1.0

0.1.0.0

Model validation for Laravel application.

  Sources   Download

by Kumaravel