Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T13843 numericality #14085

Merged
merged 5 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
- Fixed `Phalcon\Mvc\Model::writeAttribute()` to handle associative arrays correctly. [#14021](https://github.com/phalcon/cphalcon/issues/14021)
- Fixed `Phalcon\Html\Tag::appendTitle()` and `Phalcon\Html\Tag::prependTitle()`to mirror `Phalcon\Tag`. [#14039](https://github.com/phalcon/cphalcon/issues/14039)
- Fixed `Phalcon\Validation::validate()` with `cancelOnFail`. The validator will validate all elements and will stop processing validators on a per element basis if `cancelOnFail` is present. [#13149](https://github.com/phalcon/cphalcon/issues/13149)
- Fixed `Phalcon\Validation\Validator\Numericality::validate()` to parse non `en` locale numbers. [#13843](https://github.com/phalcon/cphalcon/issues/13843)

## Removed
- Removed `arrayHelpers` property from the Volt compiler. [#13925](https://github.com/phalcon/cphalcon/pull/13925)
Expand Down
11 changes: 8 additions & 3 deletions phalcon/Validation/Validator/Numericality.zep
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ class Numericality extends Validator
*/
public function validate(<Validation> validation, var field) -> bool
{
var value, message, label, replacePairs, code;
var code, label, message, replacePairs, value;
string pattern;

let value = validation->getValue(field);
// Dump spaces in the string if we have any
let value = validation->getValue(field),
value = (string) value,
value = str_replace(" ", "", value),
pattern = "/((^[-]?[0-9,]+(.[0-9]+)?$)|(^[-]?[0-9.]+(,[0-9]+)?$))/";

if !preg_match("/^-?\d+(?:[\.,]\d+)?$/", value) || !is_numeric(value) {
if !preg_match(pattern, value) {
let label = this->prepareLabel(validation, field);

let message = this->prepareMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

namespace Phalcon\Test\Integration\Validation\Validator\Numericality;

use Codeception\Example;
use IntegrationTester;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Numericality;
use stdClass;

/**
* Class ValidateCest
Expand All @@ -22,13 +26,51 @@ class ValidateCest
/**
* Tests Phalcon\Validation\Validator\Numericality :: validate()
*
* @dataProvider getExamples
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
*/
public function validationValidatorNumericalityValidate(IntegrationTester $I)
public function validationValidatorNumericalityValidate(IntegrationTester $I, Example $example)
{
$I->wantToTest('Validation\Validator\Numericality - validate()');
$I->wantToTest('Validation\Validator\Numericality - validate() ' . $example[0]);

$entity = new stdClass();
$entity->price = $example[0];

$validation = new Validation();
$validation->setEntity($entity);
$validator = new Numericality();

$I->skipTest('Need implementation');
$actual = $validator->validate($validation, 'price');
$I->assertEquals($actual, $example[1]);
}

private function getExamples(): array
{
return [
[1, true],
[123, true],
[123.45, true],
['1 234.56', true],
['1,234.56', true],
['1.23', true],
['1.123,56', true],
['1 234.56aa', false],
['1,234.56aa', false],
['1.23aa', false],
['1.123,56aa', false],
[-1, true],
[-123, true],
[-123.45, true],
['-1 234.56', true],
['-1,234.56', true],
['-1.23', true],
['-1.123,56', true],
['-1 234.56aa', false],
['-1,234.56aa', false],
['-1.23aa', false],
['-1.123,56aa', false],
];
}
}
93 changes: 0 additions & 93 deletions tests/integration/Validation/Validator/NumericalityCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,99 +124,6 @@ public function validationValidatorMultipleField(IntegrationTester $I)
$I->assertEquals($expected, $actual);
}

/**
* Tests numericality validator for custom locales
*
* @link https://github.com/phalcon/cphalcon/issues/13450
*
* @author Andrey Izman <[email protected]>
* @since 2018-08-08
*/
public function validationValidatorLocales(IntegrationTester $I)
{
$validation = new Validation();

$validation->add(
'amount',
new Numericality()
);

// get default locale
$locale = setlocale(LC_ALL, 0);

$this->setTestLocale('en_US.UTF8');


$messages = $validation->validate(
[
'amount' => 123.12,
]
);

$I->assertEquals(
0,
$messages->count()
);


$messages = $validation->validate(
[
'amount' => '123.12',
]
);

$I->assertEquals(
0,
$messages->count()
);


$messages = $validation->validate(
[
'amount' => '123,12',
]
);

$I->assertEquals(
1,
$messages->count()
);


$this->setTestLocale('fr_FR.UTF8');


$messages = $validation->validate(
[
'amount' => 123.12,
]
);

$I->assertEquals(
0,
$messages->count()
);


// revert back locale
$this->setTestLocale($locale);
}

/**
* Set locale
*
* @author Andrey Izman <[email protected]>
* @since 2018-08-08
*/
protected function setTestLocale(string $locale): string
{
putenv('LC_ALL=' . $locale);
putenv('LANG=' . $locale);
putenv('LANGUAGE=' . $locale);

return setlocale(LC_ALL, $locale);
}

private function validationValidatorSingleFieldProvider(): array
{
return [
Expand Down
31 changes: 0 additions & 31 deletions tests/unit/Translate/Adapter/Csv/UnderscoreCest.php

This file was deleted.