Skip to content

Commit 0584604

Browse files
committed
fix(integer): fix validity checks
Signed-off-by: btry <tbugier@teclib.com>
1 parent fc58377 commit 0584604

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

inc/fields/integerfield.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function isValid() {
104104
}
105105

106106
public function isValidValue() {
107-
if (!empty($value) && !ctype_digit($value)) {
107+
if (!empty($this->value) && !ctype_digit($this->value)) {
108108
Session::addMessageAfterRedirect(__('This is not an integer:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
109109
return false;
110110
}
@@ -115,7 +115,7 @@ public function isValidValue() {
115115
if (!$parameters['regex']->isNewItem()) {
116116
$regex = $parameters['regex']->fields['regex'];
117117
if ($regex !== null && strlen($regex) > 0) {
118-
if (!preg_match($regex, $value)) {
118+
if (!preg_match($regex, $this->value)) {
119119
Session::addMessageAfterRedirect(__('Specific format does not match:', 'formcreator') . ' ' . $this->fields['name'], false, ERROR);
120120
return false;
121121
}
@@ -126,13 +126,13 @@ public function isValidValue() {
126126
if (!$parameters['range']->isNewItem()) {
127127
$rangeMin = $parameters['range']->fields['range_min'];
128128
$rangeMax = $parameters['range']->fields['range_max'];
129-
if (strlen($rangeMin) > 0 && $value < $rangeMin) {
129+
if (strlen($rangeMin) > 0 && $this->value < $rangeMin) {
130130
$message = sprintf(__('The following number must be greater than %d:', 'formcreator'), $rangeMin);
131131
Session::addMessageAfterRedirect($message . ' ' . $this->fields['name'], false, ERROR);
132132
return false;
133133
}
134134

135-
if (strlen($rangeMax) > 0 && $value > $rangeMax) {
135+
if (strlen($rangeMax) > 0 && $this->value > $rangeMax) {
136136
$message = sprintf(__('The following number must be lower than %d:', 'formcreator'), $rangeMax);
137137
Session::addMessageAfterRedirect($message . ' ' . $this->fields['name'], false, ERROR);
138138
return false;

tests/suite-unit/PluginFormcreatorIntegerField.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,10 @@ public function testIsValid($fields, $data, $expectedValue, $expectedValidity) {
197197
$question->add($fields);
198198
$question->updateParameters($fields);
199199

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

202-
$isValid = $fieldInstance->isValid($fields['default_values']);
203+
$isValid = $instance->isValid();
203204
$this->boolean((boolean) $isValid)->isEqualTo($expectedValidity);
204205
}
205206
}

0 commit comments

Comments
 (0)