Permalink
Browse files

Update checkRequired to simply check if an array key is present rathe…

…r than isset (which fails if the value is null)
  • Loading branch information...
1 parent 9007406 commit fa759231da1d7d9f1d3c9c246a4c6bbaeadd9b50 @mrmorris mrmorris committed with lorenzo Nov 2, 2012
@@ -163,9 +163,9 @@ public function isRequired() {
*/
public function checkRequired($field, &$data) {
return (
- (!isset($data[$field]) && $this->isRequired() === true) ||
+ (!array_key_exists($field, $data) && $this->isRequired() === true) ||
(
- isset($data[$field]) && (empty($data[$field]) &&
+ array_key_exists($field, $data) && (empty($data[$field]) &&
!is_numeric($data[$field])) && $this->allowEmpty === false
)
);
@@ -171,4 +171,30 @@ public function testIsEmptyAllowed() {
$Rule->isUpdate(true);
$this->assertTrue($Rule->isEmptyAllowed());
}
+
+/**
+ * Test checkRequired method
+ *
+ * @return void
+ */
+ public function testCheckRequiredWhenRequiredAndAllowEmpty() {
+
+ $Rule = $this->getMock('CakeValidationRule', array('isRequired'));
+ $Rule->expects($this->any())
+ ->method('isRequired')
+ ->will($this->returnValue(true));
+ $Rule->allowEmpty = true;
+
+ $fieldname = 'field';
+ $data = array(
+ $fieldname => null
+ );
+
+ $this->assertFalse($Rule->checkRequired($fieldname, $data), "A null but present field should not fail requirement check if allowEmpty is true");
+
+ $Rule->allowEmpty = false;
+
+ $this->assertTrue($Rule->checkRequired($fieldname, $data), "A null but present field should fail requirement check if allowEmpty is false");
+
+ }
}

0 comments on commit fa75923

Please sign in to comment.