Commit a79cc546 authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

mvc, add setMultiple() to OptionField, closes https://github.com/opnsense/core/issues/1693

(cherry picked from commit ac75ef6f)
parent c849de0b
......@@ -30,6 +30,7 @@
namespace OPNsense\Base\FieldTypes;
use Phalcon\Validation\Validator\InclusionIn;
use OPNsense\Base\Validators\CsvListValidator;
/**
* Class OptionField
......@@ -58,6 +59,11 @@ class OptionField extends BaseField
*/
private $internalOptionList = array();
/**
* @var bool field may contain multiple interfaces at once
*/
private $internalMultiSelect = false;
/**
* set descriptive text for empty value
* @param $value description
......@@ -88,6 +94,19 @@ class OptionField extends BaseField
}
}
/**
* select if multiple interfaces may be selected at once
* @param $value boolean value 0/1
*/
public function setMultiple($value)
{
if (trim(strtoupper($value)) == "Y") {
$this->internalMultiSelect = true;
} else {
$this->internalMultiSelect = false;
}
}
/**
* get valid options, descriptions and selected value
* @return array
......@@ -96,11 +115,14 @@ class OptionField extends BaseField
{
$result = array ();
// if relation is not required, add empty option
if (!$this->internalIsRequired) {
if (!$this->internalIsRequired && !$this->internalMultiSelect) {
$result[""] = array("value"=>$this->internalEmptyDescription, "selected" => 0);
}
// explode options
$options = explode(',', $this->internalValue);
foreach ($this->internalOptionList as $optKey => $optValue) {
if ($optKey == $this->internalValue) {
if (in_array($optKey, $options)) {
$selected = 1;
} else {
$selected = 0;
......@@ -120,9 +142,16 @@ class OptionField extends BaseField
{
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalMultiSelect) {
// field may contain more than one option
$validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
'domain'=>array_keys($this->internalOptionList)));
} else {
// single option selection
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys($this->internalOptionList)));
}
}
return $validators;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment