Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented countable interface for ModelValidator

  • Loading branch information...
commit 3199b9029bd085f3efb31174aa1000616d9e952e 1 parent 81c0c3d
@lorenzo lorenzo authored
View
13 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, IteratorAggregate {
+class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
/**
* Holds the CakeValidationSet objects array
@@ -479,4 +479,15 @@ public function getIterator() {
$this->_parseRules();
return new ArrayIterator($this->_fields);
}
+
+/**
+ * Returns the numbers of fields having validation rules
+ *
+ * @return int
+ **/
+ public function count() {
+ $this->_parseRules();
+ return count($this->_fields);
+ }
+
}
View
29 lib/Cake/Test/Case/Model/ModelValidationTest.php
@@ -1688,7 +1688,6 @@ public function testSetValidationDomain() {
* @return void
*/
public function testGetModel() {
- $this->loadFixtures('Article', 'Comment');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1702,7 +1701,6 @@ public function testGetModel() {
* @return void
*/
public function testArrayAccessGet() {
- $this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1731,7 +1729,6 @@ public function testArrayAccessGet() {
* @return void
*/
public function testArrayAccessExists() {
- $this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1747,7 +1744,6 @@ public function testArrayAccessExists() {
* @return void
*/
public function testArrayAccessSet() {
- $this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1780,7 +1776,6 @@ public function testArrayAccessSet() {
* @return void
*/
public function testArrayAccessUset() {
- $this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1795,7 +1790,6 @@ public function testArrayAccessUset() {
* @return void
*/
public function testIterator() {
- $this->loadFixtures('Article');
$TestModel = new Article();
$Validator = $TestModel->validator();
@@ -1816,4 +1810,27 @@ public function testIterator() {
$this->assertEquals(3, $i);
}
+/**
+ * Tests countable interface in ModelValidator
+ *
+ * @return void
+ */
+ public function testCount() {
+ $TestModel = new Article();
+ $Validator = $TestModel->validator();
+ $this->assertCount(3, $Validator);
+
+ $set = array(
+ 'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
+ 'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
+ );
+ $Validator['other'] = $set;
+ $this->assertCount(4, $Validator);
+
+ unset($Validator['title']);
+ $this->assertCount(3, $Validator);
+ unset($Validator['body']);
+ $this->assertCount(2, $Validator);
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.