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.

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()
		// You can also replace this with static::creating or static::updating
			if ($model::$autoValidate)
				return $model->validate();

	public function validate()

How To Use

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


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

'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',

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 :


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