Aweloquent Model self-validation in laravel

The Aweloquent Model self-validation feature known as the operation-based version in laravel ,

In order to implement the advanced version of the self-validation system, we have to define the rule format for every model.

In this specific one, I have chosen something like this:

<?php namespace App;

use App\Aweloquent\AweloquentModel;

class Author extends AweloquentModel {

  protected $fillable = [
    'first_name', 'last_name', 'bio'
  ];

  protected static $rules = [
    'everytime' => [
      'first_name' => 'required'
    ],

    'create' => [
      'last_name' => 'required'
    ],

    'update' => [
      'bio' => 'required'
    ],
  ];

  protected static $messages = [
    'first_name.required' => 'You forgot the first name!',
    'last_name.required' => 'You forgot the last name!',
    'bio.required' => 'You forgot the biography!'
  ];

}

The $message property is left untouched. The only one that has to be modified is $rules, as you may easily imagine.

Recommended :  Introduction to promise API with $q in angularjs for beginners

In this new version of $rules, you can define rules for a single operation, 'create', or both. If you want to use a rule in both of them, there is a dedicated 'everytime' item to avoid the duplication of rules.

Of course, we have to edit our AweloquentModel once again. This time, we have to define a method that has to work with the existing validate one and understand if we are creating or updating it.

Then, merge the right rules in a single array and validate the model against those rules.

Let’s see what we can do! Consider the following code:

<?php



class AweloquentModel extends Model {

  ...

  public function __construct(array $attributes = [])
  {
    // constructor remains the same…
  }

  public function validate()
  {
    static::$rules = $this->mergeValidationRules();

    $validator = Validator::make($this->attributes, static::$rules, static::$messages);

    if($validator->fails())
    {
      $this->errors = $validator->messages();
      return false;
    }

    return true;
  }

  private function mergeValidationRules()
  {
    // if updating, use "update" rules, "create" otherwise.
    if($this->exists)
      $mergedRules = array_merge_recursive(static::$rules['everytime'], static::$rules
      ['update']);
  else
    $mergedRules = array_merge_recursive(static::$rules['everytime'], static::$rules

    ['create']);

  $finalRules = [];

  foreach($mergedRules as $field => $rules){
    if(is_array($rules))
      $finalRules[$field] = implode("|", $rules);
    else
      $finalRules[$field] = $rules;
    }

    return $finalRules;
  }

}

Great, we have it!

Recommended :  Retrieve data from Google Analytics in laravel 5.1

The validate() method doesn’t change too much. The only big difference stands in the newly line:

static::$rules = $this->mergeValidationRules();

Basically, we are saying, “Ok, now assign to the $rules property the result of this mergeValidationRules() method.”

Then, in the mergeValidationRules() method, the first instruction to be used is:

if($this->exists)

That is used to determine if the current operation is an insert or an update. Starting from this value, we can get the right rules array, merging them with the everytime rules.

Your new complex self-validating model is almost ready to be used.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares