mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Implementing IteratorAggregate interface for ModelValidator
This commit is contained in:
parent
d35f8e6b00
commit
81c0c3d91d
2 changed files with 37 additions and 1 deletions
|
@ -27,7 +27,7 @@ App::uses('CakeValidationSet', 'Model/Validator');
|
||||||
* @package Cake.Model
|
* @package Cake.Model
|
||||||
* @link http://book.cakephp.org/2.0/en/data-validation.html
|
* @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
|
* Holds the CakeValidationSet objects array
|
||||||
|
@ -470,4 +470,13 @@ class ModelValidator implements ArrayAccess {
|
||||||
unset($this->_fields[$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 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
$this->assertFalse(isset($Validator['title']));
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue