Last active
December 20, 2015 07:29
-
-
Save JesseObrien/6093609 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
should work, too:</p>