Created
February 3, 2015 10:47
-
-
Save setkyar/a5aae9aae219afc93e57 to your computer and use it in GitHub Desktop.
Laravel Validation on Model
This file contains 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
/** | |
* Create a new Elegant class | |
* And use that class from your model | |
*/ | |
class Elegant extends Eloquent | |
{ | |
protected $rules = array(); | |
protected $errors; | |
public function validate($data) | |
{ | |
// make a new validator object | |
$v = Validator::make($data, $this->rules); | |
// check for failure | |
if ($v->fails()) | |
{ | |
// set errors and return false | |
$this->errors = $v->errors(); | |
return false; | |
} | |
// validation pass | |
return true; | |
} | |
public function errors() | |
{ | |
return $this->errors; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment