Created
June 14, 2012 14:37
-
-
Save jordanthomas/2930740 to your computer and use it in GitHub Desktop.
Laravel Valdiated
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
<?php | |
Class Validated extends Eloquent { | |
public $rules = array(); | |
public $errors; | |
public function __construct($attributes = array(), $exists = false) | |
{ | |
$this->errors = new \Laravel\Messages; | |
parent::__construct($attributes, $exists); | |
} | |
public function save() | |
{ | |
if ($this->is_valid()) | |
{ | |
return parent::save(); | |
} | |
else | |
{ | |
// TODO: Valdiation exception obj. | |
throw new Exception('Validation error'); | |
} | |
} | |
public function is_valid() | |
{ | |
$validator = Validator::make($this->to_array(), $this->rules); | |
if ($validator->fails()) | |
{ | |
$this->errors = $validator->errors; | |
return FALSE; | |
} | |
else | |
{ | |
return TRUE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment