diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/UniqueIdField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/UniqueIdField.php index f179cee76..ca4ee9aff 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/UniqueIdField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/UniqueIdField.php @@ -1,7 +1,7 @@ internalValue) && empty($this->initialValue)) { - // trigger initial value on change, before returning validators - // (new nodes will always be marked as "changed", see isFieldChanged()) - // Maybe we should add an extra event handler if this kind of scenarios happen more often, similar to - // actionPostLoadingEvent. (which is not triggered on setting data for a complete new structure node) - $this->internalValue = uniqid('', true); - $this->initialValue = $this->internalValue; + if (empty((string)$this) && $this->fieldLoaded) { + parent::setValue(uniqid('', true)); + } elseif (empty((string)$this) && !$this->fieldLoaded) { + parent::setValue($value); } - $validators = parent::getValidators(); - // unique id may not change.. - $validators[] = new InclusionIn([ - 'message' => gettext('Unique ID is immutable.'), - 'domain' => [$this->initialValue], - ]); - return $validators; } + + /** + * {@inheritdoc} + */ + protected function actionPostLoadingEvent() + { + parent::actionPostLoadingEvent(); + $this->fieldLoaded = true; + } + + /** + * {@inheritdoc} + */ + public function applyDefault() + { + /** When cloned (add), set our default to a new uniqueid */ + $this->fieldLoaded = true; + $this->setValue(null); + } + }