diff --git a/lib/Cake/Model/ModelValidator.php b/lib/Cake/Model/ModelValidator.php index 692041954..0116910ad 100644 --- a/lib/Cake/Model/ModelValidator.php +++ b/lib/Cake/Model/ModelValidator.php @@ -27,7 +27,7 @@ App::uses('CakeValidationSet', 'Model/Validator'); * @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 @@ class ModelValidator implements ArrayAccess { 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); + } } diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index 0b5c84c95..b71d0858f 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1789,4 +1789,31 @@ class ModelValidationTest extends BaseModelTest { $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); + } + }