Skip to content

Commit

Permalink
Implement convert option to Phalcon\Validation\Validator\Uniqueness. F…
Browse files Browse the repository at this point in the history
…ixes #12005.
  • Loading branch information
basilfx committed Jul 26, 2016
1 parent b444ff4 commit e401134
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
- Fixed wildcard inheritance in `Phalcon\Acl\Adapter\Memory` [#12004](https://github.com/phalcon/cphalcon/issues/12004)
- Dropped support of Oracle [#12008](https://github.com/phalcon/cphalcon/issues/12008)
- Improved `Phalcon\Mvc\Collection::findById`. Added check if a `id` in a valid format [#12010](https://github.com/phalcon/cphalcon/issues/12010)
- Added `convert` option to `Phalcon\Validation\Validator\Uniqueness` to convert values to do the database lookup [#12005](https://github.com/phalcon/cphalcon/issues/12005)

# [2.0.13](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.13) (2016-05-19)
- Restored `Phalcon\Text::camelize` behavior [#11767](https://github.com/phalcon/cphalcon/issues/11767)
Expand Down
33 changes: 30 additions & 3 deletions phalcon/validation/validator/uniqueness.zep
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ use Phalcon\Mvc\Model;
* <code>
* $validator->add(['firstName', 'lastName'], new UniquenessValidator());
* </code>
*
* It is possible to convert values before validation. This is useful in
* situations where values need to be converted to do the database lookup:
* <code>
* $validator->add('username', new UniquenessValidator([
* 'convert' => function (array $values) {
* $values['username'] = strtolower($values['username']);
*
* return $values;
* }
* ]));
* </code>
*/
class Uniqueness extends CombinedFieldsValidator
{
Expand Down Expand Up @@ -90,7 +102,7 @@ class Uniqueness extends CombinedFieldsValidator

protected function isUniqueness(<Validation> validation, var field) -> boolean
{
var value, record, attribute, except,
var value, values, convert, record, attribute, except,
index, params, metaData, primaryField, className, singleField, fieldExcept, singleExcept, notInValues, exceptConditions;

let exceptConditions = [];
Expand All @@ -106,11 +118,26 @@ class Uniqueness extends CombinedFieldsValidator
let field[] = singleField;
}

let values = [];
let convert = this->getOption("convert");

for singleField in field {
let values[singleField] = validation->getValue(singleField);
}

if convert != null {
let values = {convert}(values);

if !is_array(values) {
throw new Exception("Value conversion must return an array");
}
}

for singleField in field {
let fieldExcept = null;
let notInValues = [];
let value = validation->getValue(singleField),
record = this->getOption("model");
let record = this->getOption("model");
let value = values[singleField];

if empty record || typeof record != "object" {
// check validation getEntity() method
Expand Down
67 changes: 67 additions & 0 deletions tests/unit/Validation/Validator/UniquenessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ public function testSingleField()
});
}

/**
* Tests uniqueness validator with single fields and a converted value
*
* @author Bas Stottelaar <[email protected]>
* @since 2016-07-25
*/
public function testSingleFieldConvert()
{
$this->specify('Test uniqueness with single field and a converted value.', function () {
$validation = new Validation();
$validation->add('type', new Uniqueness([
'convert' => function (array $values) {
$values['type'] = 'hydraulic'; // mechanical -> hydraulic
return $values;
}
]));
$messages = $validation->validate(null, $this->robot);
expect($messages->count())->equals(0);
});
}

/**
* Tests uniqueness validator with single field and a null value
*
Expand Down Expand Up @@ -107,6 +128,27 @@ public function testMultipleFields()
});
}

/**
* Tests uniqueness validator with multiple fields and a converted value
*
* @author Bas Stottelaar <[email protected]>
* @since 2016-07-25
*/
public function testMultipleFieldsConvert()
{
$this->specify('Test uniqueness with combination of fields and a converted value.', function () {
$validation = new Validation();
$validation->add(['name', 'type'], new Uniqueness([
'convert' => function (array $values) {
$values['type'] = 'hydraulic'; // mechanical -> hydraulic
return $values;
}
]));
$messages = $validation->validate(null, $this->robot);
expect($messages->count())->equals(0);
});
}

/**
* Tests uniqueness validator with multiple fields and a null value
*
Expand Down Expand Up @@ -213,6 +255,31 @@ public function testExceptMultipleFieldMultipleExcept()
});
}

/**
* Tests value conversion for returning an array.
*
* @author Bas Stottelaar <[email protected]>
* @since 2016-07-25
*/
public function testConvertArrayReturnsArray()
{
$this->specify('Test value conversion to return an array.', function () {
$validation = new Validation();
$validation->add('type', new Uniqueness([
'convert' => function (array $values) {
($values);
return null;
}
]));
try {
$validation->validate(null, $this->robot);
verify_that(false);
} catch (\Exception $e) {
verify_that(true);
}
});
}

/**
* Initialize data for the tests
*/
Expand Down

0 comments on commit e401134

Please sign in to comment.