From 091b075476ca44db0b0e9ba64cf16c2863cc6d20 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 23 May 2017 18:17:40 +0200 Subject: [PATCH] mvc, model. Validation issue when storing a value for the first time. If a field isn't required and the user sets it with a faulty value, it would pass because it didn't know it wasn't set before user input. There where two issues, first one, not knowing it was in init state (solved with internalFieldLoaded), next all field types should have used actionPostLoadingEvent to customize loading behaviour. --- .../models/OPNsense/Base/FieldTypes/AuthGroupField.php | 2 +- .../Base/FieldTypes/AuthenticationServerField.php | 2 +- .../app/models/OPNsense/Base/FieldTypes/BaseField.php | 10 ++++++++-- .../OPNsense/Base/FieldTypes/CertificateField.php | 2 +- .../OPNsense/Base/FieldTypes/ConfigdActionsField.php | 2 +- .../models/OPNsense/Base/FieldTypes/CountryField.php | 2 +- .../models/OPNsense/Base/FieldTypes/InterfaceField.php | 2 +- .../app/models/OPNsense/Base/FieldTypes/PortField.php | 2 +- 8 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php index 174a846b2..fc5e57023 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthGroupField.php @@ -71,7 +71,7 @@ class AuthGroupField extends BaseField /** * generate validation data (list of certificates) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (empty(self::$internalOptionList)) { $cnf = Config::getInstance()->object(); diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthenticationServerField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthenticationServerField.php index 4d4d12086..921e7894d 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthenticationServerField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/AuthenticationServerField.php @@ -74,7 +74,7 @@ class AuthenticationServerField extends BaseField /** * generate validation data (list of AuthServers) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (!isset(self::$internalOptionList[$this->internalCacheKey])) { self::$internalOptionList[$this->internalCacheKey] = array(); 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 1825d941e..51ff545ba 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php @@ -112,6 +112,11 @@ abstract class BaseField */ private $internalChangeCase = null; + /** + * @var bool is field loaded (after post loading event) + */ + private $internalFieldLoaded = false; + /** * generate a new UUID v4 number * @return string uuid v4 number @@ -150,6 +155,7 @@ abstract class BaseField $node->eventPostLoading(); } $this->actionPostLoadingEvent(); + $this->internalFieldLoaded = true; } /** @@ -287,8 +293,8 @@ abstract class BaseField */ public function setValue($value) { - // if first set, store initial value - if ($this->internalInitialValue === false && $value != "") { + // if first set and not altered by the user, store initial value + if ($this->internalFieldLoaded === false && $this->internalInitialValue === false && $value != "") { $this->internalInitialValue = $value; } $this->internalValue = $value; diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CertificateField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CertificateField.php index ce933d4dd..091b11c2b 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CertificateField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CertificateField.php @@ -96,7 +96,7 @@ class CertificateField extends BaseField /** * generate validation data (list of certificates) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (!isset(self::$internalOptionList[$this->certificateType])) { self::$internalOptionList[$this->certificateType] = array(); diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ConfigdActionsField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ConfigdActionsField.php index a94b9586c..4a190b076 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ConfigdActionsField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/ConfigdActionsField.php @@ -66,7 +66,7 @@ class ConfigdActionsField extends BaseField /** * generate validation data (list of known configd actions) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (!isset(self::$internalOptionList[$this->internalCacheKey])) { self::$internalOptionList[$this->internalCacheKey] = array(); diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CountryField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CountryField.php index f5090cbb9..e77391fe3 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CountryField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/CountryField.php @@ -66,7 +66,7 @@ class CountryField extends BaseField /** * generate validation data (list of countries) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (count(self::$internalOptionList) == 0) { $filename = '/usr/local/opnsense/contrib/tzdata/iso3166.tab'; diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php index 6f02529e4..41d06e4da 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/InterfaceField.php @@ -120,7 +120,7 @@ class InterfaceField extends BaseField /** * generate validation data (list of interfaces and well know ports) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (!isset(self::$internalOptionList[$this->internalCacheKey])) { self::$internalOptionList[$this->internalCacheKey] = array(); diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/PortField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/PortField.php index f7401d6b5..9323fd36a 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/PortField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/PortField.php @@ -101,7 +101,7 @@ class PortField extends BaseField /** * generate validation data (list of port numbers and well know ports) */ - public function eventPostLoading() + protected function actionPostLoadingEvent() { if (!is_array(self::$internalOptionList)) { self::$internalOptionList = array("any") + self::$wellknownservices;