Giter Site home page Giter Site logo

theriddleofenigma / laravel-model-validation Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 5.0 57 KB

Laravel model validation validates the data on model events. *Only for laravel applications.

Home Page: https://theriddleofenigma.github.io/laravel-model-validation/

License: MIT License

PHP 100.00%
laravel php model eloquent eloquent-models validation model-validation laravel-framework laravel-application database

laravel-model-validation's Introduction

โ™ฅ Made with <love/> And I love <code/>

Laravel Model Validation

FOSSA Status

Model validation - Validates the model data. *Only for laravel applications.

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

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.

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.

/**
 * 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.

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

/**
 * 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.

License

Laravel Model Validation is open-sourced software licensed under the MIT license.

FOSSA Status

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.