Created
December 3, 2017 20:27
-
-
Save robert-moore96/8017549c56ab3a68784d8eb9ac66a751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Validator; | |
trait ValidatesOnSave | |
{ | |
protected static function bootValidatesOnSave() | |
{ | |
static::saving(function ($model) { | |
if (method_exists($model, 'setDefaultValues')) { | |
$model->setDefaultValues(); | |
} | |
$model->validate(); | |
}); | |
} | |
public function validation() | |
{ | |
throw new \Exception("Model must override 'validation()' method with intended rules"); | |
} | |
public function validate() | |
{ | |
Validator::make($this->attributes, $this->validation())->validate(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment