syehan/laravel-model-validation

There is no license information available for the latest version (dev-main) of this package.

Model validation for Laravel application.

dev-main 2022-12-28 09:56 UTC

This package is auto-updated.

Last update: 2024-10-28 13:41:24 UTC


README

♥ 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..
    }
}

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