PHP code example of theriddleofenigma / laravel-model-validation

1. Go to this page and download the library: Download theriddleofenigma/laravel-model-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

theriddleofenigma / laravel-model-validation example snippets


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' => '*/
    public function beforeValidation()
    {
        // Some code goes here..
    }

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

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

/**
 * 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' => 'nd return the messages as however you want.
    return [
        'name.

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