Skip to content
Permalink
Browse files

Implementing IteratorAggregate interface for ModelValidator

  • Loading branch information...
lorenzo committed May 5, 2012
1 parent d35f8e6 commit 81c0c3d91d05cab428cadd5983304aec15c726c1
Showing with 37 additions and 1 deletion.
  1. +10 −1 lib/Cake/Model/ModelValidator.php
  2. +27 −0 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -27,7 +27,7 @@
* @package Cake.Model
* @link http://book.cakephp.org/2.0/en/data-validation.html
*/
class ModelValidator implements ArrayAccess {
class ModelValidator implements ArrayAccess, IteratorAggregate {
/**
* Holds the CakeValidationSet objects array
@@ -470,4 +470,13 @@ public function offsetUnset($field) {
unset($this->_fields[$field]);
}
/**
* Returns an iterator for each of the fields to be validated
*
* @return ArrayIterator
**/
public function getIterator() {
$this->_parseRules();
return new ArrayIterator($this->_fields);
}
}
@@ -1789,4 +1789,31 @@ public function testArrayAccessUset() {
$this->assertFalse(isset($Validator['title']));
}
/**
* Tests it is possible to iterate a validation object
*
* @return void
*/
public function testIterator() {
$this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
$i = 0;
foreach ($Validator as $field => $rules) {
if ($i === 0) {
$this->assertEquals('user_id', $field);
}
if ($i === 1) {
$this->assertEquals('title', $field);
}
if ($i === 2) {
$this->assertEquals('body', $field);
}
$this->assertInstanceOf('CakeValidationSet', $rules);
$i++;
}
$this->assertEquals(3, $i);
}
}

0 comments on commit 81c0c3d

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