Skip to content

Commit

Permalink
fix(selectfield): validity check different from radios field
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Feb 23, 2021
1 parent 36870e5 commit 46ce9b3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
26 changes: 26 additions & 0 deletions inc/field/selectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

use Dropdown;
use Html;
use Session;
use Toolbox;

class SelectField extends RadiosField
{
Expand Down Expand Up @@ -88,6 +90,30 @@ public static function getName(): string {
return __('Select', 'formcreator');
}

public function isValid(): bool {
// If the field is required it can't be empty
if ($this->isRequired() && $this->value == '0') {
Session::addMessageAfterRedirect(
sprintf(__('A required field is empty: %s', 'formcreator'), $this->getLabel()),
false,
ERROR
);
return false;
}

// All is OK
return $this->isValidValue($this->value);
}

public function isValidValue($value): bool {
if ($value == '0') {
return true;
}
$value = Toolbox::stripslashes_deep($value);
$value = trim($value);
return in_array($value, $this->getAvailableValues());
}

public function equals($value): bool {
if ($value == '') {
// empty string means no selection
Expand Down
28 changes: 14 additions & 14 deletions tests/3-unit/GlpiPlugin/Formcreator/Field/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public function providerIsValid() {
'order' => '1',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
],
'expectedValue' => '1',
'expectedIsValid' => true
'value' => '1',
'expected' => true
],
[
'fields' => [
Expand All @@ -155,8 +155,8 @@ public function providerIsValid() {
'order' => '1',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
],
'expectedValue' => '',
'expectedIsValid' => true
'value' => '0',
'expected' => true
],
[
'fields' => [
Expand All @@ -169,8 +169,8 @@ public function providerIsValid() {
'order' => '1',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
],
'expectedValue' => '3',
'expectedIsValid' => true
'value' => '3',
'expected' => true
],
[
'fields' => [
Expand All @@ -183,8 +183,8 @@ public function providerIsValid() {
'order' => '1',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
],
'expectedValue' => '1',
'expectedIsValid' => true
'value' => '1',
'expected' => true
],
[
'fields' => [
Expand All @@ -197,22 +197,22 @@ public function providerIsValid() {
'order' => '1',
'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_ALWAYS
],
'expectedValue' => '',
'expectedIsValid' => false
'value' => '0',
'expected' => false
],
];
}

/**
* @dataProvider providerIsValid
*/
public function testIsValid($fields, $expectedValue, $expected) {
public function testIsValid($fields, $value, $expected) {
$question = $this->getQuestion($fields);
$instance = $this->newTestedInstance($question);
$instance->deserializeValue($fields['default_values']);
$instance->deserializeValue($value);

$isValid = $instance->isValid();
$this->boolean((boolean) $isValid)->isEqualTo($expected);
$output = $instance->isValid();
$this->boolean((boolean) $output)->isEqualTo($expected);
}

public function testGetName() {
Expand Down

0 comments on commit 46ce9b3

Please sign in to comment.