Laravel Form Automatic model validation Example

Today, We want to share with you Laravel Form Automatic model validation Example.In this post we will show you form validation in model in laravel, hear for where to put validation in laravel we will give you demo and example for implement.In this post, we will learn about validation in model or controller with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Laravel Form Automatic model validation Example

There are the Following The simple About Laravel Form Automatic model validation Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Simple Laravel 6 model auto-validation, so the laravel model is used for this example is following below.

Automatic model validation

class Post extends Eloquent
{
	public static $autoValidate = true;
	protected static $rules = array();

	protected static function boot()
	{
		parent::boot();
		// You can also replace this with static::creating or static::updating
		static::saving(function($model)
		{
			if ($model::$autoValidate)
			{
				return $model->validate();
			}
		});
	}

	public function validate()
	{
	}
}

How To Use

class Post extends Model
{
  protected $fillable = [
    // fillables
  ];
 
  public function validate()
  {
    // logic here
  }
}

Implementation

Event::listen('eloquent.saving:*', function ($model) {
  $reflector = new ReflectionClass($model);
  if ($reflector->hasMethod('validate')) {
    $validator = $reflector->getMethod('validate');
    return $validator->invoke($model);
  }
  return true;
});

Laravel update model with unique validation rule for attribute

//rules
'email' => 'unique:users,email_address,' . $userId,

In your Laravel 6 model, create a static function:

public static function rules ($id=0, $merge=[]) {
    return array_merge(
        [
            'username'  => 'required|min:3|max:12|unique:users,username' . ($id ? ",$id" : ''),
            'email'     => 'required|email|unique:member'. ($id ? ",id,$id" : ''),
            'firstname' => 'required|min:2',
            'lastname'  => 'required|min:2',
            ...
        ], 
        $merge);
}

Validation on create:

$validator = Validator::make($input, User::rules());

Validation on update:

$validator = Validator::make($input, User::rules($id));

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about form validation in model in laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Vue js Axios HTTP Post Method Example Tutorial From Scratch
  2.   $timeout Service using Angular Example
  3.   Stopping On First Validation Failure in Laravel Bail Rule
  4.   Ionic Side Menus - Side Menu and Navigation
  5.   Laravel 6 After Today Validation Tutorial Example
  6.   Simple Laravel CRUD Validation Tutorial
  7.   Simple Laravel Form Validation Example
  8.   Simple Vuejs Currency Converter Example
  9.   Create Bootable USB Drive Steps - Bootable Pen Drive
  10.   Laravel 7 Form Validation rules Tutorial Example