Skip to content

Commit

Permalink
Implemented allowEmpty for validator
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 20, 2013
1 parent a2699a9 commit 65553e4
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 45 deletions.
43 changes: 1 addition & 42 deletions Cake/ORM/Validation/ValidationSet.php
Expand Up @@ -47,13 +47,6 @@ class ValidationSet implements \ArrayAccess, \IteratorAggregate, \Countable {
*/
protected $_validationDomain = null;

/**
* Holds the fieldname
*
* @var string
*/
public $field = null;

/**
* Holds the original ruleSet
*
Expand Down Expand Up @@ -98,7 +91,7 @@ public function isPresenceRequired($validatePresent = null) {
* @param boolean|string $allowEmpty Valid values are true, false, 'create', 'update'
* @return boolean|string
*/
public function allowEmpty($allowEmpty = null) {
public function isEmptyAllowed($allowEmpty = null) {
if ($allowEmpty === null) {
return $this->_allowEmpty;
}
Expand Down Expand Up @@ -146,40 +139,6 @@ public function validate($data, $isUpdate = false) {
return $errors;
}

/**
* Returns whether the field can be left blank according to `allowEmpty`
*
* @return boolean
*/
public function isEmptyAllowed() {
if (in_array($this->_allowEmpty, array('create', 'update'), true)) {
return (
($this->_allowEmpty === 'create' && !$this->_isUpdate) ||
($this->_allowEmpty === 'update' && $this->_isUpdate)
);
}

return $this->_allowEmpty;
}


/**
* Checks if the `allowEmpty` property applies
*
* @param string $field Field to check
* @param array $data data to check against
* @return boolean
*/
public function checkEmpty($field, $data) {
if (!array_key_exists($field, $data)) {
return false;
}
if (empty($data[$field]) && $data[$field] != '0' && $this->isEmptyAllowed()) {
return true;
}
return false;
}

/**
* Gets a rule for a given name if exists
*
Expand Down
57 changes: 54 additions & 3 deletions Cake/ORM/Validator.php
Expand Up @@ -58,6 +58,11 @@ public function errors(array $data, $newRecord = true) {
if (!$keyPresent && !$this->_checkPresence($field, $newRecord)) {
$errors[$name][] = __d('cake', 'This field is required');
}
if ($keyPresent && !$this->_checkEmpty($field, $newRecord)) {
if ($this->_fieldIsEmpty($data[$name])) {
$errors[$name][] = __d('cake', 'This field cannot be left empty');
}
}
}

return $errors;
Expand Down Expand Up @@ -221,11 +226,24 @@ public function remove($field, $rule = null) {
* Sets whether a field is required to be present in data array.
*
* @param string $field the name of the field
* @param boolean|string $allowEmpty Valid values are true, false, 'create', 'update'
* @param boolean|string $mode Valid values are true, false, 'create', 'update'
* @return Validator this instance
*/
public function validatePresence($field, $mode = true) {
$this->field($field)->isPresenceRequired($mode);
return $this;
}

/**
* Sets whether a field is allowed to be empty. If it is, all other validation
* rules will be ignored
*
* @param string $field the name of the field
* @param boolean|string $mode Valid values are true, false, 'create', 'update'
* @return Validator this instance
*/
public function validatePresence($field, $type = true) {
$this->field($field)->isPresenceRequired($type);
public function allowEmpty($field, $mode = true) {
$this->field($field)->isEmptyAllowed($mode);
return $this;
}

Expand All @@ -249,4 +267,37 @@ protected function _checkPresence($field, $newRecord) {
return !$required;
}

/**
*
* Returns whether the field can be left blank according to `allowEmpty`
*
* @param ValidationSet $field the set of rules for a field
* @param boolean $newRecord whether the data to be validated is new or to be updated.
* @return boolean
*/
protected function _checkEmpty($field, $newRecord) {
$allowed = $field->isEmptyAllowed();
if (in_array($allowed, array('create', 'update'), true)) {
$allowed = (
($allowed === 'create' && $newRecord) ||
($allowed === 'update' && !$newRecord)
);
}

return $allowed;
}

/**
* Returns true if the field is empty in the passed data array
*
* @param mixed $data value to check against
* @return boolean
*/
protected function _fieldIsEmpty($data) {
if (empty($data) && $data !== '0' && $data !== false && $data !== 0) {
return true;
}
return false;
}

}
76 changes: 76 additions & 0 deletions Cake/Test/TestCase/ORM/ValidatorTest.php
Expand Up @@ -127,7 +127,83 @@ public function testErrorsWithPresenceRequired() {

$validator->validatePresence('title', false);
$this->assertEmpty($validator->errors(['foo' => 'bar']));
}

/**
* Tests the allowEmpty method
*
* @return void
*/
public function testAllowEmpty() {
$validator = new Validator;
$this->assertSame($validator, $validator->allowEmpty('title'));
$this->assertTrue($validator->field('title')->isEmptyAllowed());

$validator->allowEmpty('title', false);
$this->assertFalse($validator->field('title')->isEmptyAllowed());

$validator->allowEmpty('title', 'created');
$this->assertEquals('created', $validator->field('title')->isEmptyAllowed());

$validator->allowEmpty('title', 'updated');
$this->assertEquals('updated', $validator->field('title')->isEmptyAllowed());
}

/**
* Tests errors generated when a field is not allowed to be empty
*
* @return void
*/
public function testErrorsWithEmptyNotAllowed() {
$validator = new Validator;
$validator->allowEmpty('title', false);
$errors = $validator->errors(['title' => '']);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);

$errors = $validator->errors(['title' => []]);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);

$errors = $validator->errors(['title' => null]);
$expected = ['title' => ['This field cannot be left empty']];
$this->assertEquals($expected, $errors);

$errors = $validator->errors(['title' => 0]);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => '0']);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => false]);
$this->assertEmpty($errors);
}

/**
* Tests errors generated when a field is allowed to be empty
*
* @return void
*/
public function testErrorsWithEmptyAllowed() {
$validator = new Validator;
$validator->allowEmpty('title');
$errors = $validator->errors(['title' => '']);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => []]);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => null]);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => 0]);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => '0']);
$this->assertEmpty($errors);

$errors = $validator->errors(['title' => false]);
$this->assertEmpty($errors);
}

}

0 comments on commit 65553e4

Please sign in to comment.