Commit 78ca1710 authored by Ad Schellevis's avatar Ad Schellevis

Merge pull request #869 from fraenki/pr_868

support multiple values in ModelRelationField, fixes #868
parents c5c70b93 4f871e40
......@@ -30,6 +30,7 @@
namespace OPNsense\Base\FieldTypes;
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.
......@@ -42,6 +43,11 @@ class ModelRelationField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var bool field may contain multiple data nodes at once
*/
private $internalMultiSelect = false;
/**
* @var string default validation message string
*/
......@@ -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
* @return array
......@@ -121,8 +140,9 @@ class ModelRelationField extends BaseField
$result[""] = array("value"=>"none", "selected" => 0);
}
$datanodes = explode(',', $this->internalValue);
foreach (self::$internalOptionList[$this->internalCacheKey] as $optKey => $optValue) {
if ($optKey == $this->internalValue && $this->internalValue != null) {
if (in_array($optKey, $datanodes)) {
$selected = 1;
} else {
$selected = 0;
......@@ -142,14 +162,9 @@ class ModelRelationField extends BaseField
{
$validators = parent::getValidators();
if ($this->internalValue != null) {
if (array_key_exists($this->internalCacheKey, self::$internalOptionList) &&
count(self::$internalOptionList[$this->internalCacheKey]) > 0) {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain' => array_keys(self::$internalOptionList[$this->internalCacheKey])));
} else {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain' => array()));
}
// field may contain more than one entries
$validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
}
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