Skip to content

Commit 20b9b01

Browse files
committed
Add Validator::hasField()
This will be useful in form building as fields without validation sets should not default to required.
1 parent f36b167 commit 20b9b01

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/Validation/Validator.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ public function field($name, ValidationSet $set = null) {
112112
return $this->_fields[$name];
113113
}
114114

115+
/**
116+
* Check whether or not a validator contains any rules for the given field.
117+
*
118+
* @param string $name The field name to check.
119+
* @return boolean
120+
*/
121+
public function hasField($name) {
122+
return isset($this->_fields[$name]);
123+
}
124+
115125
/**
116126
* Associates an object to a name so it can be used as a provider. Providers are
117127
* objects or class names that can contain methods used during validation of for

tests/TestCase/Validation/ValidatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ public function testAddingRulesToField() {
5050
*/
5151
public function testFieldDefault() {
5252
$validator = new Validator;
53+
$this->assertFalse($validator->hasField('foo'));
54+
5355
$field = $validator->field('foo');
5456
$this->assertInstanceOf('\Cake\Validation\ValidationSet', $field);
5557
$this->assertCount(0, $field);
58+
$this->assertTrue($validator->hasField('foo'));
5659
}
5760

5861
/**

0 commit comments

Comments
 (0)