Skip to content

Commit

Permalink
Merge pull request #12519 from gguridi/feature/allow-empty-values
Browse files Browse the repository at this point in the history
allowEmpty can be populated with the list of what we consider 'empty'. The validation only will stop if the empty validation returns true.
  • Loading branch information
sergeyklay authored Jan 10, 2017
2 parents 0d06761 + 5efa0ab commit 27eb58f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added afterBinding event to `Phalcon\Dispatcher` and `Phalcon\Mvc\Micro`, added `Phalcon\Mvc\Micro::afterBinding`
- Added the ability to set custom Resultset class returned by find() [#12166](https://github.com/phalcon/cphalcon/issues/12166)
- Added the ability to clear appended and prepended title elements (Phalcon\Tag::appendTitle, Phalcon\Tag::prependTitle). Now you can use array to add multiple titles. For more details check [#12238](https://github.com/phalcon/cphalcon/issues/12238).
- Added the ability to specify what empty means in the 'allowEmpty' option of the validators. Now it accepts as well an array specifying what's empty, for example ['', false]

# [3.0.4](https://github.com/phalcon/cphalcon/releases/tag/v3.0.4) (XXXX-XX-XX)
- Fixed Isnull check is not correct when the model field defaults to an empty string. [#12507](https://github.com/phalcon/cphalcon/issues/12507)
Expand Down
24 changes: 16 additions & 8 deletions phalcon/validation.zep
Original file line number Diff line number Diff line change
Expand Up @@ -579,23 +579,31 @@ class Validation extends Injectable implements ValidationInterface
*/
protected function preChecking(var field, <ValidatorInterface> validator) -> boolean
{
var singleField;
var singleField, allowEmpty, emptyValue, value, result;
if typeof field == "array" {
for singleField in field {
if validator->getOption("allowEmpty", false) {
if method_exists(validator, "isAllowEmpty") {
return validator->isAllowEmpty(this, singleField);
}
return empty this->getValue(singleField);
let result = this->preChecking(singleField, validator);
if result {
return result;
}
}
}
else {
if validator->getOption("allowEmpty", false) {
let allowEmpty = validator->getOption("allowEmpty", false);
if allowEmpty {
if method_exists(validator, "isAllowEmpty") {
return validator->isAllowEmpty(this, field);
}
return empty this->getValue(field);
let value = this->getValue(field);
if typeof allowEmpty == "array" {
for emptyValue in allowEmpty {
if emptyValue === value {
return true;
}
}
return false;
}
return empty value;
}
}

Expand Down
62 changes: 62 additions & 0 deletions tests/unit/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,66 @@ function () {
}
);
}

/**
* Tests that empty values behaviour.
*
* @author Gorka Guridi <[email protected]>
* @since 2016-12-30
*/
public function testEmptyValues()
{
$this->specify(
"Validation of empty values doesn't work as expected",
function () {
$validation = new Validation();

$validation->setDI(new FactoryDefault());

$validation
->add('name', new Validation\Validator\Alpha(array(
'message' => 'The name is not valid',
)))
->add('name', new Validation\Validator\PresenceOf(array(
'message' => 'The name is required',
)))
->add('url', new Validation\Validator\Url(array(
'message' => 'The url is not valid.',
'allowEmpty' => true,
)))
->add('email', new Validation\Validator\Email(array(
'message' => 'The email is not valid.',
'allowEmpty' => [null, false],
)));

$messages = $validation->validate([
'name' => '',
'url' => null,
'email' => '',
]);
expect($messages)->count(2);

$messages = $validation->validate([
'name' => 'MyName',
'url' => '',
'email' => '',
]);
expect($messages)->count(1);

$messages = $validation->validate([
'name' => 'MyName',
'url' => false,
'email' => null,
]);
expect($messages)->count(0);

$messages = $validation->validate([
'name' => 'MyName',
'url' => 0,
'email' => 0,
]);
expect($messages)->count(1);
}
);
}
}

0 comments on commit 27eb58f

Please sign in to comment.