Skip to content

Instantly share code, notes, and snippets.

@JesseObrien
Last active December 20, 2015 07:29
Show Gist options
  • Save JesseObrien/6093609 to your computer and use it in GitHub Desktop.
Save JesseObrien/6093609 to your computer and use it in GitHub Desktop.
<?php
class ValidationModel extends Eloquent {
/**
* Hold the validation rules for the model
*
* @var array
*/
protected $rules = array();
/**
* Hold the validator instance
*
* @var Validator
*/
protected $validator = null;
/**
* Whether or not we want to check validity on save
*
* @var bool
*/
protected $checkValid = true;
/**
* Get the rules for this model
*
* @return array
*/
public function getRules()
{
return $this->rules;
}
/**
* Check to see if the attributes on this model are valid
*
* @return bool
*/
public function isValid()
{
$this->validator = Validator::make($this->attributes, $this->getRules());
return $this->validator->passes();
}
/**
* Return the validator instance on this model if there is one
*
* @return null | Validator
*/
public function getValidator()
{
return $this->validator;
}
/**
* Override save to provide an optional validity check
*
* @param array
*
* @return mixed
*/
public function save(array $options = array())
{
if ($this->checkValid and ! $this->isValid())
{
return false;
}
return parent::save($options);
}
/**
* Bypass the validation check
*
* return Model
*/
public function bypassValidation()
{
$this->checkValid = false;
return $this;
}
}
Copy link

ghost commented Oct 30, 2013

should work, too:</p>

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