Commit 4f871e40 authored by Frank Wall's avatar Frank Wall

support multiple values in ModelRelationField, fixes #868

parent 95522e09
...@@ -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 ModelRelationField defines a relation to another entity within the model, acts like a select item. * Class ModelRelationField defines a relation to another entity within the model, acts like a select item.
...@@ -42,6 +43,11 @@ class ModelRelationField extends BaseField ...@@ -42,6 +43,11 @@ class ModelRelationField extends BaseField
*/ */
protected $internalIsContainer = false; protected $internalIsContainer = false;
/**
* @var bool field may contain multiple data nodes at once
*/
private $internalMultiSelect = false;
/** /**
* @var string default validation message string * @var string default validation message string
*/ */
...@@ -107,6 +113,19 @@ class ModelRelationField extends BaseField ...@@ -107,6 +113,19 @@ class ModelRelationField extends BaseField
} }
} }
/**
* select if multiple data nodes may be selected at once
* @param $value boolean value Y/N
*/
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
...@@ -121,8 +140,9 @@ class ModelRelationField extends BaseField ...@@ -121,8 +140,9 @@ class ModelRelationField extends BaseField
$result[""] = array("value"=>"none", "selected" => 0); $result[""] = array("value"=>"none", "selected" => 0);
} }
$datanodes = explode(',', $this->internalValue);
foreach (self::$internalOptionList[$this->internalCacheKey] as $optKey => $optValue) { foreach (self::$internalOptionList[$this->internalCacheKey] as $optKey => $optValue) {
if ($optKey == $this->internalValue && $this->internalValue != null) { if (in_array($optKey, $datanodes)) {
$selected = 1; $selected = 1;
} else { } else {
$selected = 0; $selected = 0;
...@@ -142,14 +162,9 @@ class ModelRelationField extends BaseField ...@@ -142,14 +162,9 @@ class ModelRelationField extends BaseField
{ {
$validators = parent::getValidators(); $validators = parent::getValidators();
if ($this->internalValue != null) { if ($this->internalValue != null) {
if (array_key_exists($this->internalCacheKey, self::$internalOptionList) && // field may contain more than one entries
count(self::$internalOptionList[$this->internalCacheKey]) > 0) { $validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage, 'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
'domain' => array_keys(self::$internalOptionList[$this->internalCacheKey])));
} else {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain' => array()));
}
} }
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