Commit ac75ef6f authored by Ad Schellevis's avatar Ad Schellevis

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

parent 0502860e
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
namespace OPNsense\Base\FieldTypes; namespace OPNsense\Base\FieldTypes;
use Phalcon\Validation\Validator\InclusionIn; use Phalcon\Validation\Validator\InclusionIn;
use OPNsense\Base\Validators\CsvListValidator;
/** /**
* Class OptionField * Class OptionField
...@@ -58,6 +59,11 @@ class OptionField extends BaseField ...@@ -58,6 +59,11 @@ class OptionField extends BaseField
*/ */
private $internalOptionList = array(); private $internalOptionList = array();
/**
* @var bool field may contain multiple interfaces at once
*/
private $internalMultiSelect = false;
/** /**
* set descriptive text for empty value * set descriptive text for empty value
* @param $value description * @param $value description
...@@ -88,6 +94,19 @@ class OptionField extends BaseField ...@@ -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 * get valid options, descriptions and selected value
* @return array * @return array
...@@ -96,11 +115,14 @@ class OptionField extends BaseField ...@@ -96,11 +115,14 @@ class OptionField extends BaseField
{ {
$result = array (); $result = array ();
// if relation is not required, add empty option // if relation is not required, add empty option
if (!$this->internalIsRequired) { if (!$this->internalIsRequired && !$this->internalMultiSelect) {
$result[""] = array("value"=>$this->internalEmptyDescription, "selected" => 0); $result[""] = array("value"=>$this->internalEmptyDescription, "selected" => 0);
} }
// explode options
$options = explode(',', $this->internalValue);
foreach ($this->internalOptionList as $optKey => $optValue) { foreach ($this->internalOptionList as $optKey => $optValue) {
if ($optKey == $this->internalValue) { if (in_array($optKey, $options)) {
$selected = 1; $selected = 1;
} else { } else {
$selected = 0; $selected = 0;
...@@ -120,8 +142,15 @@ class OptionField extends BaseField ...@@ -120,8 +142,15 @@ class OptionField extends BaseField
{ {
$validators = parent::getValidators(); $validators = parent::getValidators();
if ($this->internalValue != null) { if ($this->internalValue != null) {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage, if ($this->internalMultiSelect) {
'domain'=>array_keys($this->internalOptionList))); // 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; 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