Home » Laravel Form Automatic model validation Example

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.

Read Also:  jQuery Add and Remove Multiple Div Dynamically using PHP

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

Read Also:  Simple Laravel CRUD Validation Tutorial

//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 :

Read Also:  Ng-Template using Angular Example

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. Read Also:  PHP Mail Attachment - Php Send Email File-Images Attachments
  2. Read Also:  Simple Laravel Form Validation Example
  3. Read Also:  Login with Facebook and Twitter using PHP
  4. Read Also:  WooCommerce Get Product Downloads
  5. Read Also:  Retrieve Data From MySQL Database using Vue.js PHP
  6. Read Also:  jQuery Ajax GET & POST REQUEST Methods PHP MySQLi
  7. Read Also:  PHP Laravel 6 Form Validation Script
  8. Read Also:  Laravel 7 Form Validation rules Tutorial Example
  9. Read Also:  Angular 9/8 Material Select Box Examples
  10. Read Also:  Stopping On First Validation Failure in Laravel Bail Rule

Leave a Comment

Your email address will not be published. Required fields are marked *