Skip to content

Commit

Permalink
Add Validator::hasField()
Browse files Browse the repository at this point in the history
This will be useful in form building as fields without validation sets
should not default to required.
  • Loading branch information
markstory committed Feb 2, 2014
1 parent f36b167 commit 20b9b01
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Validation/Validator.php
Expand Up @@ -112,6 +112,16 @@ public function field($name, ValidationSet $set = null) {
return $this->_fields[$name];
}

/**
* Check whether or not a validator contains any rules for the given field.
*
* @param string $name The field name to check.
* @return boolean
*/
public function hasField($name) {
return isset($this->_fields[$name]);
}

/**
* Associates an object to a name so it can be used as a provider. Providers are
* objects or class names that can contain methods used during validation of for
Expand Down
3 changes: 3 additions & 0 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -50,9 +50,12 @@ public function testAddingRulesToField() {
*/
public function testFieldDefault() {
$validator = new Validator;
$this->assertFalse($validator->hasField('foo'));

$field = $validator->field('foo');
$this->assertInstanceOf('\Cake\Validation\ValidationSet', $field);
$this->assertCount(0, $field);
$this->assertTrue($validator->hasField('foo'));
}

/**
Expand Down

0 comments on commit 20b9b01

Please sign in to comment.