Skip to content

Commit 46ce9b3

Browse files
committed
fix(selectfield): validity check different from radios field
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent 36870e5 commit 46ce9b3

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

inc/field/selectfield.class.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
use Dropdown;
3636
use Html;
37+
use Session;
38+
use Toolbox;
3739

3840
class SelectField extends RadiosField
3941
{
@@ -88,6 +90,30 @@ public static function getName(): string {
8890
return __('Select', 'formcreator');
8991
}
9092

93+
public function isValid(): bool {
94+
// If the field is required it can't be empty
95+
if ($this->isRequired() && $this->value == '0') {
96+
Session::addMessageAfterRedirect(
97+
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
98+
false,
99+
ERROR
100+
);
101+
return false;
102+
}
103+
104+
// All is OK
105+
return $this->isValidValue($this->value);
106+
}
107+
108+
public function isValidValue($value): bool {
109+
if ($value == '0') {
110+
return true;
111+
}
112+
$value = Toolbox::stripslashes_deep($value);
113+
$value = trim($value);
114+
return in_array($value, $this->getAvailableValues());
115+
}
116+
91117
public function equals($value): bool {
92118
if ($value == '') {
93119
// empty string means no selection

tests/3-unit/GlpiPlugin/Formcreator/Field/SelectField.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public function providerIsValid() {
141141
'order' => '1',
142142
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
143143
],
144-
'expectedValue' => '1',
145-
'expectedIsValid' => true
144+
'value' => '1',
145+
'expected' => true
146146
],
147147
[
148148
'fields' => [
@@ -155,8 +155,8 @@ public function providerIsValid() {
155155
'order' => '1',
156156
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
157157
],
158-
'expectedValue' => '',
159-
'expectedIsValid' => true
158+
'value' => '0',
159+
'expected' => true
160160
],
161161
[
162162
'fields' => [
@@ -169,8 +169,8 @@ public function providerIsValid() {
169169
'order' => '1',
170170
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
171171
],
172-
'expectedValue' => '3',
173-
'expectedIsValid' => true
172+
'value' => '3',
173+
'expected' => true
174174
],
175175
[
176176
'fields' => [
@@ -183,8 +183,8 @@ public function providerIsValid() {
183183
'order' => '1',
184184
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
185185
],
186-
'expectedValue' => '1',
187-
'expectedIsValid' => true
186+
'value' => '1',
187+
'expected' => true
188188
],
189189
[
190190
'fields' => [
@@ -197,22 +197,22 @@ public function providerIsValid() {
197197
'order' => '1',
198198
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
199199
],
200-
'expectedValue' => '',
201-
'expectedIsValid' => false
200+
'value' => '0',
201+
'expected' => false
202202
],
203203
];
204204
}
205205

206206
/**
207207
* @dataProvider providerIsValid
208208
*/
209-
public function testIsValid($fields, $expectedValue, $expected) {
209+
public function testIsValid($fields, $value, $expected) {
210210
$question = $this->getQuestion($fields);
211211
$instance = $this->newTestedInstance($question);
212-
$instance->deserializeValue($fields['default_values']);
212+
$instance->deserializeValue($value);
213213

214-
$isValid = $instance->isValid();
215-
$this->boolean((boolean) $isValid)->isEqualTo($expected);
214+
$output = $instance->isValid();
215+
$this->boolean((boolean) $output)->isEqualTo($expected);
216216
}
217217

218218
public function testGetName() {

0 commit comments

Comments
 (0)