Skip to content

Instantly share code, notes, and snippets.

@jordanthomas
Created June 14, 2012 14:37
Show Gist options
  • Save jordanthomas/2930740 to your computer and use it in GitHub Desktop.
Save jordanthomas/2930740 to your computer and use it in GitHub Desktop.
Laravel Valdiated
<?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