From 0b6317accb896944d8632d4f02a2aabc6ea4a789 Mon Sep 17 00:00:00 2001 From: Stephan de Wit Date: Tue, 22 Aug 2023 08:50:45 +0200 Subject: [PATCH] MVC: Add allowEmpty option to UniqueConstraint This is useful in cases where "empty" has special meaning, but is allowed to occur multiple times, while any set value still has to adhere to the unique constraint. --- .../OPNsense/Base/Constraints/UniqueConstraint.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php b/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php index 2c194d6e1..1390084c6 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php @@ -45,9 +45,18 @@ class UniqueConstraint extends BaseConstraint */ public function validate($validator, $attribute): bool { + $allowEmpty = ($this->getOption('allowEmpty') === 'Y') ? true : false; + if ($allowEmpty && !empty($this->getOptionValueList('addFields'))) { + throw new \Exception('UniqueConstraint allowEmpty and addFields cannot be used in tandem'); + } + $node = $this->getOption('node'); $fieldSeparator = chr(10) . chr(0); if ($node) { + if ($allowEmpty && empty((string)$node)) { + return true; + } + $containerNode = $node; $nodeName = $node->getInternalXMLTagName(); $parentNode = $node->getParentNode();