Skip to content

Instantly share code, notes, and snippets.

@emsifa
Last active August 29, 2015 14:13
Show Gist options
  • Save emsifa/0aaa07c08c4fbdeb120b to your computer and use it in GitHub Desktop.
Save emsifa/0aaa07c08c4fbdeb120b to your computer and use it in GitHub Desktop.
Laravel Simple ValidateableModelTrait
<?php
class ExampleController extends BaseController {
public function addUser()
{
$user = User::create(Input::all());
// if validation not passes
if(false == $user->isSaved()) {
$error_messages = $user->getErrorsValidation();
return Redirect::back()->withErrors($error_messages)->withInput();
}
// user sucessfully saved...
return Redirect::route('login');
}
}
<?php
// example user model
class User extends Eloquent {
use ValidateableModelTrait;
protected $fillable = ['username', 'password'];
protected function getRules()
{
return [
'username' => 'required|min:6|max:16',
'password' => 'required'
];
}
protected function getInsertRules()
{
return [
'username' => 'required|min:6|max:16|unique:users'
];
}
}
<?php
trait ValidateableModelTrait {
protected $rules = array();
protected $rules_insert = array();
protected $rules_update = array();
protected $validation_errors = array();
protected $validator = null;
protected $ignored_validation_attributes = array();
protected $is_saved;
/**
* Override create method from eloquent model
*
* @param attributes set of filled attributes
* @param custom_rules for inject custom validation rules
* @param custom_messages custom error validation messages
* @return Model
*/
public static function create(
array $attributes,
array $custom_rules = array(),
array $custom_messages = array()
)
{
$model = new static($attributes);
$model->save(array('merge_values' => $attributes), $custom_rules, $custom_messages);
return $model;
}
/**
* Override save method from eloquent model
*
* @param array options (from eloquent model)
* @param array custom_rules for inject(or modify) custom rules
* @param array custom_messages validation custom error messages
* @return Model
*/
public function save(
array $options = array(),
array $custom_rules = array(),
array $custom_messages = array()
)
{
$this->is_saved = false;
$rules = $this->getRules();
if( ! $this->exists) {
$rules = array_merge($rules, $this->getInsertRules());
} else {
$rules = array_merge($rules, $this->getUpdateRules());
}
$rules = array_merge($rules, $custom_rules);
if( ! empty($rules)) {
$merge_values = isset($options['merge_values'])? $options['merge_values'] : array();
$values = array_merge($this->attributes, array_except($merge_values, array_keys($this->attributes)));
foreach($this->ignored_validation_attributes as $attr)
{
unset($values[$attr]);
unset($rules[$attr]);
}
$this->validator = \Validator::make($values, $rules, $custom_messages);
if($this->validator->fails()) {
$this->validation_errors = $this->validator->messages();
return $this;
}
$this->ignored_validation_attributes = array();
}
$this->validator = null;
$model = parent::save($options);
if($model) {
$this->is_saved = true;
}
return $this;
}
/**
* Set ignored validation attributes
*
* @param array attributes to ignored
*/
public function ignoreValidationAttributes(array $attributes)
{
$this->ignored_validation_attributes = array_merge($this->ignored_validation_attributes, $attributes);
}
/**
* Check if model successfuly saved (validation passes and model saved)
*
* @return boolean
*/
public function isSaved()
{
return $this->is_saved;
}
/**
* Get laravel validator object (only after validating model)
*
* @return Validator or null
*/
public function getValidator()
{
return $this->validator;
}
/**
* Get error validation
*
* @return array error messages
*/
public function getErrorsValidation()
{
return $this->validation_errors;
}
/**
* Get general validation rules
*/
public function getRules()
{
return $this->rules;
}
/**
* Get update rules
*/
public function getUpdateRules()
{
return $this->rules_update;
}
/**
* Get insert rules
*/
public function getInsertRules()
{
return $this->rules_insert;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment