Last active
October 12, 2015 14:08
-
-
Save ADmad/4038473 to your computer and use it in GitHub Desktop.
Selective validation behavior
This file contains hidden or 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 | |
/** | |
* Selective validation behavior | |
* | |
* @copyright ADmad | |
* @link https://github.com/ADmad | |
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
*/ | |
class SelectiveValidationBehavior extends ModelBehavior { | |
/** | |
* Backup of orignal model validations | |
* | |
* @var array | |
*/ | |
protected $_defaultValidation = array(); | |
/** | |
* Eg. structure for $config array where array keys 'action_name1', 'action_name2' | |
* are controller action names. | |
* array( | |
* 'action_name1' => array( | |
* 'merge' => true, // See docblock of useRulesSet() for all possible values | |
* 'rules' => array( | |
* //rules in same format as $validate array | |
* ) | |
* ) | |
* 'action_name2' => array( | |
* 'merge' => 'reverse', // See docblock of useRulesSet() for all possible values | |
* 'rules' => 'arrayNameContainingRules' | |
* ) | |
* ) | |
* | |
* @param Model $Model Model object | |
* @param array $config Config array | |
*/ | |
public function setup($Model, $config = array()) { | |
$this->_defaultValidation[$Model->alias] = $Model->validate; | |
if (!empty($config)) { | |
$this->settings[$Model->alias] = $config; | |
} else { | |
$this->settings[$Model->alias] = array(); | |
} | |
} | |
/** | |
* Switch validation rules | |
* | |
* @param Model $Model Model object | |
* @return boolean | |
*/ | |
public function beforeValidate($Model) { | |
if (isset($this->settings[$Model->alias][Router::getParam('action')])) { | |
$actionSettings = $this->settings[$Model->alias][Router::getParam('action')]; | |
if (isset($actionSettings['merge']) && $actionSettings['merge']) { | |
if ($actionSettings['merge'] === true) { | |
if (is_string($actionSettings['rules'])) { | |
$Model->validate = array_merge($Model->validate, $Model->{$actionSettings['rules']}); | |
} else { | |
$Model->validate = array_merge($Model->validate, $actionSettings['rules']); | |
} | |
} else { | |
if (is_string($actionSettings['rules'])) { | |
$Model->validate = array_merge($Model->{$actionSettings['rules']}, $Model->validate); | |
} else { | |
$Model->validate = array_merge($actionSettings['rules'], $Model->validate); | |
} | |
} | |
} else { | |
if (is_string($actionSettings['rules'])) { | |
$Model->validate = $Model->{$actionSettings['rules']}; | |
} else { | |
$Model->validate = $actionSettings['rules']; | |
} | |
} | |
} | |
return true; | |
} | |
/** | |
* Set the rules set to use for validation | |
* | |
* @param Model $Model Model object | |
* @param string $rulesSetName Name to the model property containing validation rules | |
* @param mixed $merge Merging scheme. Possible values are: | |
* `'forward'|true` : Merge new rules after existing rules (field name keys from new rules overwrite older ones) | |
* `'reverse'` : Merge new rules before existing rules (field name keys from old rules overwrite new ones) | |
* `false` : Overwrite existings rules, no merging | |
* @return void | |
*/ | |
public function useRulesSet($Model, $rulesSetName, $merge = true) { | |
if ($merge) { | |
if ($merge === true || $merge === 'forward') { | |
$Model->validate = array_merge($Model->validate, $Model->{$rulesSetName}); | |
} else { | |
$Model->validate = array_merge($Model->{$rulesSetName}, $Model->validate); | |
} | |
} else { | |
$Model->validate = $Model->{$rulesSetName}; | |
} | |
} | |
/** | |
* Reset rules set to model's original Model::$validate | |
* | |
* @param Model $Model Model object | |
* @return void | |
*/ | |
public function resetRulesSet($Model) { | |
$Model->validate = $this->_defaultValidation[$Model->alias]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment