Skip to content

Instantly share code, notes, and snippets.

@J5Dev
Last active August 29, 2015 14:02
Show Gist options
  • Save J5Dev/26e6d95e2ee374b9b469 to your computer and use it in GitHub Desktop.
Save J5Dev/26e6d95e2ee374b9b469 to your computer and use it in GitHub Desktop.
Laravel Base model for use with model based validation, compensating for Unique on update
<?php
use Eloquent, Validator;
/**
* Class BaseModel
*
* @package
*/
class BaseModel extends Eloquent {
/**
* Used to store errors
* @var
*/
protected $errors;
/**
*
*/
public static function boot()
{
parent::boot();
static::creating(function($model)
{
if (! property_exists($model, 'rules'))
{
return true;
}
return $model->validateModel();
});
static::updating(function($model)
{
if (! property_exists($model, 'rules'))
{
return true;
}
return $model->validateModel(true);
});
}
/**
* Validates an object when attempting to save an instance
*
*
* @param bool $update
* @return bool
*/
public function validateModel($update = false)
{
if($update)
{
$validation = Validator::make($this->getAttributes(), $this->getValidationRules($this->id), $this->getValidationMessages());
} else {
$validation = Validator::make($this->getAttributes(), $this->getValidationRules(), $this->getValidationMessages());
}
if ($validation->fails())
{
$this->errors = $validation->messages();
return false;
}
return true;
}
/**
* Gets the errors
*
* @return mixed
*/
public function getErrors()
{
return $this->errors;
}
/**
* Get validation messages
*
* @return array
*/
public static function getValidationMessages()
{
if (! property_exists(get_called_class(), 'validationMessages'))
{
return [];
}
$validationMessages = static::$validationMessages;
return $validationMessages;
}
/**
* Get validation rules and take care of own id on update
*
* @param null $id
* @return array
*/
public static function getValidationRules($id = null)
{
if (! property_exists(get_called_class(), 'rules'))
{
return [];
}
$rules = static::$rules;
if ($id === null)
{
return $rules;
}
array_walk($rules, function(&$rules, $field) use ($id)
{
if (!is_array($rules))
{
$rules = explode("|", $rules);
}
foreach($rules as $ruleIdx => $rule)
{
// get name and parameters
@list($name, $params) = array_pad(explode(":", $rule, 2), 2, null);
// only do someting for the unique rule
if (strtolower($name) != "unique")
{
continue; // continue in foreach loop, nothing left to do here
}
$p = array_map("trim", explode(",", $params));
// set field name to rules key ($field) (laravel convention)
if (!isset($p[1]))
{
$p[1] = $field;
}
// set 3rd parameter to id given to getValidationRules()
$p[2] = $id;
$params = implode(",", $p);
$rules[$ruleIdx] = $name.":".$params;
}
});
return $rules;
}
}
<?php
use Eloquent, SoftDeletingTrait;
use BaseModel;
/**
* Class DemoModel
*/
class DemoModel extends BaseModel {
use SoftDeletingTrait;
protected $dates = ['deleted_at'];
/**
* @var string
*/
protected $table = "demos";
/**
* @var array
*/
protected $guarded = ['id', 'deleted_at', 'created_at', 'updated_at'];
/**
* @var array
*/
public static $rules = [
'slug' => 'required|alpha_dash|unique:demos'
];
/**
* @var array
*/
public static $validationMessages = [
'slug' => 'This is a demo custom message'
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment