Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit fa759231da1d7d9f1d3c9c246a4c6bbaeadd9b50 1 parent 9007406
@mrmorris mrmorris authored lorenzo committed
View
4 lib/Cake/Model/Validator/CakeValidationRule.php
@@ -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
)
);
View
26 lib/Cake/Test/Case/Model/Validator/CakeValidationRuleTest.php
@@ -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");
+
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.