Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel Base Model
<?php namespace Tmb;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Validation\Validator;
class BaseModel extends Eloquent
{
/**
* Error message bag
*
* @var Illuminate\Support\MessageBag
*/
protected $errors;
/**
* Validation rules
*
* @var Array
*/
protected static $rules = array();
/**
* Custom error messages
*
* @var array
*/
protected static $messages = array();
/**
* Validator instance
*
* @var Illuminate\Validation\Validators
*/
protected $validator;
public function __construct(array $attributes = array(), Validator $validator = null)
{
parent::__construct($attributes);
$this->validator = $validator ?: \App::make('validator');
}
/**
* Listen for save event
*/
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
return $model->validate();
});
}
/**
* Validates current attributes against rules
*/
public function validate()
{
$replace = ($this->getKey() > 0) ? $this->getKey() : '';
foreach (static::$rules as $key => $rule)
{
static::$rules[$key] = str_replace(':id', $replace, $rule);
}
$validator = $this->validator->make($this->attributes, static::$rules, static::$messages);
if ($validator->passes()) return true;
$this->setErrors($validator->messages());
return false;
}
/**
* Set error message bag
*
* @var Illuminate\Support\MessageBag
*/
protected function setErrors($errors)
{
$this->errors = $errors;
}
/**
* Retrieve error message bag
*/
public function getErrors()
{
return $this->errors;
}
/**
* Return if there are any errors
*
* @return bool
*/
public function hasErrors()
{
return ! empty($this->errors);
}
}
@jimthedev

This comment has been minimized.

Copy link

@jimthedev jimthedev commented Nov 4, 2013

@JonoB, have you considered submitting this as a pull request to Laravel so that this could be the default functionality for models? Seems like this is much better functionality for validating model updates than the current implementation.

@davidsoderberg

This comment has been minimized.

Copy link

@davidsoderberg davidsoderberg commented Jan 6, 2014

How do you use the errors in your controllers update method?

@dwightwatson

This comment has been minimized.

Copy link

@dwightwatson dwightwatson commented Apr 29, 2014

Worth noting this will not work if you use the array syntax for defining your validation rules.

@yazeed

This comment has been minimized.

Copy link

@yazeed yazeed commented Jun 10, 2014

How do we use this?

@zoul0813

This comment has been minimized.

Copy link

@zoul0813 zoul0813 commented Aug 11, 2014

This works really well and solved a problem I was having.

I modified the above code to replace '{:id}' instead of ':id', as the ':id' was conflicting with the "required_without:id' rule I had applied on another field. The use of '{}' allows me to quickly identify the ':id' as a "templated" value when scanning rules as well.

Line 65:

    static::$rules[$key] = str_replace('{:id}', $replace, $rule);
@pdcmoreira

This comment has been minimized.

Copy link

@pdcmoreira pdcmoreira commented Apr 25, 2015

This breaks on inserts for me because my rule is checked also on inserts and when the model is new and therefore has no id, it passes an empty string as the id to search for.

I've replaced $replace = ($this->getKey() > 0) ? $this->getKey() : ''; with $replace = ($this->getKey() > 0) ? $this->getKey() : 0;. Seems to fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment