Skip to content
Permalink
Browse files

Implementing ArrayAccess in ModelValidator

  • Loading branch information...
lorenzo committed May 5, 2012
1 parent b6c8a34 commit d35f8e6b006d37812669231a3f52b756a5a55b25
Showing with 146 additions and 6 deletions.
  1. +53 −6 lib/Cake/Model/ModelValidator.php
  2. +93 −0 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -18,16 +18,16 @@
* @since CakePHP(tm) v 2.2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('CakeValidationSet', 'Model/Validator');
App::uses('CakeRule', 'Model/Validator');
/**
* ModelValidator object.
*
* @package Cake.Model
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class ModelValidator {
class ModelValidator implements ArrayAccess {
/**
* Holds the CakeValidationSet objects array
@@ -290,16 +290,16 @@ public function getFields($name = null) {
* @return boolean True if Model::$validate was processed, false otherwise
*/
protected function _parseRules() {
if ($this->_validate === $this->_model->validate) {
return true;
}
if (empty($this->_model->validate)) {
$this->_validate = array();
$this->_fields = array();
return false;
}
if ($this->_validate === $this->_model->validate) {
return true;
}
$this->_validate = $this->_model->validate;
$this->_fields = array();
$methods = $this->getMethods();
@@ -423,4 +423,51 @@ protected function _triggerBeforeValidate($options = array()) {
return true;
}
/**
* Returns wheter a rule set is defined for a field or not
*
* @param string $field name of the field to check
* @return boolean
**/
public function offsetExists($field) {
$this->_parseRules();
return isset($this->_fields[$field]);
}
/**
* Returns the rule set for a field
*
* @param string $field name of the field to check
* @return CakeValidationSet
**/
public function offsetGet($field) {
$this->_parseRules();
return $this->_fields[$field];
}
/**
* Sets the rule set for a field
*
* @param string $field name of the field to set
* @param array|CakeValidationSet $rules set of rules to apply to field
* @return void
**/
public function offsetSet($field, $rules) {
$this->_parseRules();
if (!$rules instanceof CakeValidationSet) {
$rules = new CakeValidationSet($field, $rules, $this->getMethods());
}
$this->_fields[$field] = $rules;
}
/**
* Unsets the rulset for a field
*
* @param string $field name of the field to unset
* @return void
**/
public function offsetUnset($field) {
unset($this->_fields[$field]);
}
}
@@ -1696,4 +1696,97 @@ public function testGetModel() {
$this->assertInstanceOf('Article', $result);
}
/**
* Tests it is possible to get validation sets for a field using an array inteface
*
* @return void
*/
public function testArrayAccessGet() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$titleValidator = $Validator['title'];
$this->assertEquals('title', $titleValidator->field);
$this->assertCount(1, $titleValidator->getRules());
$rule = current($titleValidator->getRules());
$this->assertEquals('notEmpty', $rule->rule);
$titleValidator = $Validator['body'];
$this->assertEquals('body', $titleValidator->field);
$this->assertCount(1, $titleValidator->getRules());
$rule = current($titleValidator->getRules());
$this->assertEquals('notEmpty', $rule->rule);
$titleValidator = $Validator['user_id'];
$this->assertEquals('user_id', $titleValidator->field);
$this->assertCount(1, $titleValidator->getRules());
$rule = current($titleValidator->getRules());
$this->assertEquals('numeric', $rule->rule);
}
/**
* Tests it is possible to check for validation sets for a field using an array inteface
*
* @return void
*/
public function testArrayAccessExists() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$this->assertTrue(isset($Validator['title']));
$this->assertTrue(isset($Validator['body']));
$this->assertTrue(isset($Validator['user_id']));
$this->assertFalse(isset($Validator['other']));
}
/**
* Tests it is possible to set validation rules for a field using an array inteface
*
* @return void
*/
public function testArrayAccessSet() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$set = array(
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
);
$Validator['other'] = $set;
$rules = $Validator['other'];
$this->assertEquals('other', $rules->field);
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
$Validator['new'] = new CakeValidationSet('new', $set, array());
$rules = $Validator['new'];
$this->assertEquals('new', $rules->field);
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
}
/**
* Tests it is possible to unset validation rules
*
* @return void
*/
public function testArrayAccessUset() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$this->assertTrue(isset($Validator['title']));
unset($Validator['title']);
$this->assertFalse(isset($Validator['title']));
}
}

0 comments on commit d35f8e6

Please sign in to comment.
You can’t perform that action at this time.