From c868e0adc69c80a0ff8feefe996a400043bef212 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Mon, 26 Feb 2024 16:21:03 +0100 Subject: [PATCH] mvc - extend model implementation to support volatile fields, these fields will be validated when offered, but will not be serialized to the target xml structure. Example usage: The advantage of this is one can ask the user for input, validate it when offered and report back when it's not valid. --- .../app/models/OPNsense/Base/BaseModel.php | 3 ++ .../OPNsense/Base/FieldTypes/BaseField.php | 28 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php index a7fe59a85..e62eb1900 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php @@ -215,6 +215,9 @@ abstract class BaseModel } $fieldObject = $field_rfcls->newInstance($new_ref, $tagName); $fieldObject->setParentModel($this); + if (($xmlNode->attributes()["volatile"] ?? '') == 'true') { + $fieldObject->setInternalIsVolatile(); + } // now add content to this model (recursive) if ($fieldObject->isContainer() == false) { diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php index 329bb2f1a..900e9f590 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php @@ -1,7 +1,7 @@ internalIsVirtual; } + /** + * Mark this node as volatile + */ + public function setInternalIsVolatile() + { + $this->internalIsVolatile = true; + } + + /** + * returns if this node is volatile, the framework uses this to determine if this node should be stored. + * @return bool is volatile node + */ + public function getInternalIsVolatile() + { + return $this->internalIsVolatile; + } + /** * getter for internal tag name * @return null|string xml tagname to use @@ -687,8 +709,8 @@ abstract class BaseField } foreach ($this->iterateItems() as $key => $FieldNode) { - if ($FieldNode->getInternalIsVirtual()) { - // Virtual fields should never be persisted + if ($FieldNode->getInternalIsVirtual() || $FieldNode->getInternalIsVolatile()) { + // Virtual and volatile fields should never be persisted continue; } $FieldNode->addToXMLNode($subnode);