Posted inTechnology / Laravel / Mysql / Mysqli / php / Programming

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()
	{
		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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype