Created
August 29, 2012 01:34
-
-
Save oppara/3505924 to your computer and use it in GitHub Desktop.
MultivalidatableBehabior for CakePHP2
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 | |
/** | |
* MultivalidatableBehabior for CakePHP2 | |
* | |
* @uses ModelBehavior | |
* @author Dardo Sordi> | |
* @link http://bakery.cakephp.org/articles/dardosordi/2008/07/29/multivalidatablebehavior-using-many-validation-rulesets-per-model | |
*/ | |
class MultivalidatableBehavior extends ModelBehavior | |
{ | |
/** | |
* Stores previous validation ruleset | |
* | |
* @var array | |
* @access private | |
*/ | |
private $__old_rules = array(); | |
/** | |
* Stores Model default validation ruleset | |
* | |
* @var array | |
* @access private | |
*/ | |
private $__default_rules = array(); | |
public function setUp($Model, $config = array()) | |
{ | |
$this->__default_rules[$Model->name] = $Model->validate; | |
} | |
/** | |
* Installs a new validation ruleset | |
* | |
* If $rules is an array, it will be set as current validation ruleset, | |
* otherwise it will look into Model::validationSets[$rules] for the ruleset to install | |
* | |
* @param Model $model | |
* @param array $rules | |
* @return void | |
* @access public | |
*/ | |
public function setValidation($Model, $rules = array()) | |
{ | |
if (is_array($rules)) { | |
$this->_setValidation($Model, $rules); | |
} elseif (isset($Model->validationSets[$rules])) { | |
$this->setValidation($Model, $Model->validationSets[$rules]); | |
} | |
} | |
/** | |
* Restores previous validation ruleset | |
* | |
* @param Model $model | |
* @return void | |
* @access public | |
*/ | |
public function restoreValidation($Model) | |
{ | |
$Model->validate = $this->__old_rules[$Model->name]; | |
} | |
/** | |
* Restores default validation ruleset | |
* | |
* @param Model $model | |
* @return void | |
* @access public | |
*/ | |
public function restoreDefaultValidation($Model) | |
{ | |
$Model->validate = $this->__default_rules[$Model->name]; | |
} | |
/** | |
* Sets a new validation ruleset, saving the previous | |
* | |
* @param Model $model | |
* @param array $rules | |
* @return void | |
* @access protected | |
*/ | |
protected function _setValidation($Model, $rules) | |
{ | |
$this->__old_rules[$Model->name] = $Model->validate; | |
$Model->validate = $rules; | |
} | |
} |
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 | |
App::import('Behavior', 'Multivalidatable'); | |
class MultivalidatablePost extends CakeTestModel | |
{ | |
public $useTable = false; | |
public $actsAs = array( | |
'Multivalidatable', | |
); | |
public $validate = array( | |
'mail' => array( | |
'require' => array( | |
'rule' => array( | |
'notempty', | |
), | |
'required' => true, | |
'last' => true, | |
), | |
'email' => array( | |
'rule' => array( | |
'email', | |
), | |
), | |
), | |
'password' => array( | |
'require' => array( | |
'rule' => array( | |
'notempty', | |
), | |
'required' => true, | |
'last' => true, | |
'on' => 'create', | |
), | |
), | |
'name' => array( | |
'require' => array( | |
'rule' => array( | |
'notempty', | |
), | |
'required' => true, | |
'last' => true, | |
), | |
), | |
); | |
public $validationSets = array( | |
'foo' => array( | |
'name' => array( | |
'require' => array( | |
'rule' => array( | |
'notempty', | |
), | |
'required' => true, | |
'last' => true, | |
), | |
), | |
), | |
'bar' => array( | |
'status' => array( | |
'boolean' => array( | |
'rule' => array( | |
'boolean', | |
), | |
'required' => true, | |
'last' => true, | |
), | |
), | |
'name' => array( | |
'require' => array( | |
'rule' => array( | |
'notempty', | |
), | |
'required' => true, | |
'last' => true, | |
), | |
), | |
), | |
); | |
} | |
/** | |
* Multivalidatable Test case | |
*/ | |
class MultivalidatableBehaviorTest extends CakeTestCase | |
{ | |
public function setUp() | |
{ | |
$this->Model = new MultivalidatablePost(); | |
} | |
public function tearDown() | |
{ | |
unset($this->Model); | |
ClassRegistry::flush(); | |
} | |
public function testルールを切り替えた後、もとに戻せること事() | |
{ | |
$this->_checkDefaultRule(); | |
$this->Model->setValidation('foo'); | |
$this->_checkFooRule(); | |
$this->Model->restoreValidation(); | |
$this->_checkDefaultRule(); | |
} | |
public function testルールを複数回切り替えた後、もとに戻せること事() | |
{ | |
$this->_checkDefaultRule(); | |
$this->Model->setValidation('foo'); | |
$this->_checkFooRule(); | |
$this->Model->setValidation('bar'); | |
$rules = $this->Model->validate; | |
$this->assertEqual(count($rules), 2); | |
$this->assertTrue(isset($rules['name'])); | |
$this->assertTrue(isset($rules['status'])); | |
$this->Model->restoreValidation(); | |
$this->_checkFooRule(); | |
$this->Model->restoreDefaultValidation(); | |
$this->_checkDefaultRule(); | |
} | |
protected function _checkFooRule() | |
{ | |
$rules = $this->Model->validate; | |
$this->assertEqual(count($rules), 1); | |
$this->assertTrue(isset($rules['name'])); | |
} | |
protected function _checkDefaultRule() | |
{ | |
$rules = $this->Model->validate; | |
$this->assertEqual(count($rules), 3); | |
$this->assertTrue(isset($rules['mail'])); | |
$this->assertTrue(isset($rules['password'])); | |
$this->assertTrue(isset($rules['name'])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment