diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php new file mode 100644 index 000000000..cd5b3419b --- /dev/null +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ModelRelationField.php @@ -0,0 +1,129 @@ +getNodeByReference($modelData['items'])->__items as $node) { + $displayKey = $modelData['display']; + if (array_key_exists("uuid", $node->getAttributes()) && $node->$displayKey != null) { + $uuid = $node->getAttributes()['uuid']; + self::$internalOptionList[$uuid] = $node->$displayKey->__toString(); + } + } + } + unset($modelObj); + } + } + } + } + } + + /** + * get valid options, descriptions and selected value + * @return array + */ + public function getNodeData() + { + $result = array (); + if (is_array(self::$internalOptionList)) { + foreach (self::$internalOptionList as $optKey => $optValue) { + if ($optKey == $this->internalValue && $this->internalValue != null) { + $selected = 1; + } else { + $selected = 0; + } + $result[$optKey] = array("value"=>$optValue, "selected" => $selected); + } + } + + return $result; + } + + /** + * @return array returns Text/regex validator + */ + public function getValidators() + { + // build validation mask + $validationMask = '('; + $countid = 0 ; + foreach (self::$internalOptionList as $key => $value) { + if ($countid > 0) { + $validationMask .= '|'; + } + $validationMask .= $key ; + $countid++; + } + $validationMask .= ')'; + + if ($this->internalValidationMessage == null) { + $msg = "option not in list" ; + } else { + $msg = $this->internalValidationMessage; + } + if (($this->internalIsRequired == true || $this->internalValue != null) && $validationMask != null) { + return array(new Regex(array('message' => $msg,'pattern'=>trim($validationMask)))); + } else { + // empty field and not required, skip this validation. + return array(); + } + } +}