Skip to content

Commit

Permalink
fix(integer): fix validity checks
Browse files Browse the repository at this point in the history
Signed-off-by: btry <tbugier@teclib.com>
  • Loading branch information
btry committed Sep 19, 2018
1 parent fc58377 commit 0584604
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions inc/fields/integerfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function isValid() {
}

public function isValidValue() {
if (!empty($value) && !ctype_digit($value)) {
if (!empty($this->value) && !ctype_digit($this->value)) {
Session::addMessageAfterRedirect(__('This is not an integer:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
return false;
}
Expand All @@ -115,7 +115,7 @@ public function isValidValue() {
if (!$parameters['regex']->isNewItem()) {
$regex = $parameters['regex']->fields['regex'];
if ($regex !== null && strlen($regex) > 0) {
if (!preg_match($regex, $value)) {
if (!preg_match($regex, $this->value)) {
Session::addMessageAfterRedirect(__('Specific format does not match:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
return false;
}
Expand All @@ -126,13 +126,13 @@ public function isValidValue() {
if (!$parameters['range']->isNewItem()) {
$rangeMin = $parameters['range']->fields['range_min'];
$rangeMax = $parameters['range']->fields['range_max'];
if (strlen($rangeMin) > 0 && $value < $rangeMin) {
if (strlen($rangeMin) > 0 && $this->value < $rangeMin) {
$message = sprintf(__('The following number must be greater than %d:', 'formcreator'), $rangeMin);
Session::addMessageAfterRedirect($message . ' ' . $this->fields['name'], false, ERROR);
return false;
}

if (strlen($rangeMax) > 0 && $value > $rangeMax) {
if (strlen($rangeMax) > 0 && $this->value > $rangeMax) {
$message = sprintf(__('The following number must be lower than %d:', 'formcreator'), $rangeMax);
Session::addMessageAfterRedirect($message . ' ' . $this->fields['name'], false, ERROR);
return false;
Expand Down
5 changes: 3 additions & 2 deletions tests/suite-unit/PluginFormcreatorIntegerField.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ public function testIsValid($fields, $data, $expectedValue, $expectedValidity) {
$question->add($fields);
$question->updateParameters($fields);

$fieldInstance = new \PluginFormcreatorIntegerField($question->fields, $data);
$instance = new \PluginFormcreatorIntegerField($question->fields, $data);
$instance->deserializeValue($fields['default_values']);

$isValid = $fieldInstance->isValid($fields['default_values']);
$isValid = $instance->isValid();
$this->boolean((boolean) $isValid)->isEqualTo($expectedValidity);
}
}

0 comments on commit 0584604

Please sign in to comment.