Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implementing IteratorAggregate interface for ModelValidator

  • Loading branch information...
commit 81c0c3d91d05cab428cadd5983304aec15c726c1 1 parent d35f8e6
@lorenzo lorenzo authored
View
11 lib/Cake/Model/ModelValidator.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);
+ }
}
View
27 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -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);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.