From b438666ea728e230a18fb7391d70505730eb10f5 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Tue, 8 Oct 2024 22:17:00 +0900 Subject: [PATCH] Validate required and strict extra vars on document insert and update --- modules/document/document.controller.php | 45 +++++++++++++++-- modules/extravar/models/Value.php | 62 ++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/modules/document/document.controller.php b/modules/document/document.controller.php index da0bbed81..60bf40e6a 100644 --- a/modules/document/document.controller.php +++ b/modules/document/document.controller.php @@ -831,7 +831,21 @@ class DocumentController extends Document { $value = trim($obj->{$extra_item->name}); } - if($value == NULL) continue; + + // Validate the extra value. + if ($value == NULL && $manual_inserted) + { + continue; + } + else + { + $ev_output = $extra_item->validate($value); + if ($ev_output && !$output->toBool()) + { + $oDB->rollback(); + return $ev_output; + } + } $extra_vars[$extra_item->name] = $value; $this->insertDocumentExtraVar($obj->module_srl, $obj->document_srl, $idx, $value, $extra_item->eid); } @@ -1164,14 +1178,35 @@ class DocumentController extends Document if(isset($obj->{'extra_vars'.$idx})) { $tmp = $obj->{'extra_vars'.$idx}; - if(is_array($tmp)) + if (is_array($tmp)) + { $value = implode('|@|', $tmp); + } else + { $value = trim($tmp); + } } - else if(isset($obj->{$extra_item->name})) $value = trim($obj->{$extra_item->name}); - if($value == NULL) continue; - $extra_vars[$extra_item->name] = $value; + elseif (isset($obj->{$extra_item->name})) + { + $value = trim($obj->{$extra_item->name}); + } + + // Validate the extra value. + if ($value == NULL && $manual_updated) + { + continue; + } + else + { + $ev_output = $extra_item->validate($value); + if ($ev_output && !$ev_output->toBool()) + { + $oDB->rollback(); + return $ev_output; + } + } + $extra_vars[$extra_item->name] = $value; $this->insertDocumentExtraVar($obj->module_srl, $obj->document_srl, $idx, $value, $extra_item->eid); } } diff --git a/modules/extravar/models/Value.php b/modules/extravar/models/Value.php index 9028de37f..a776a3164 100644 --- a/modules/extravar/models/Value.php +++ b/modules/extravar/models/Value.php @@ -2,6 +2,8 @@ namespace Rhymix\Modules\Extravar\Models; +use BaseObject; +use Context; use ModuleModel; use Rhymix\Framework\DateTime; use Rhymix\Framework\i18n; @@ -213,6 +215,66 @@ class Value return isset(self::OPTION_TYPES[$this->type]); } + /** + * Check if the current value is an array type. + * + * @return bool + */ + public function isArrayType(): bool + { + return isset(self::ARRAY_TYPES[$this->type]); + } + + /** + * Validate a value. + * + * @param mixed $value + * @return ?BaseObject + */ + public function validate($value): ?BaseObject + { + $is_array = is_array($value); + $values = is_array($value) ? $value : [$value]; + + // Check if a required value is empty. + if ($this->is_required === 'Y') + { + if ($is_array && trim(implode('', $values)) === '') + { + return new BaseObject(-1, sprintf(lang('common.filter.isnull'), Context::replaceUserLang($this->name))); + } + if (!$is_array && trim(strval($value)) === '') + { + return new BaseObject(-1, sprintf(lang('common.filter.isnull'), Context::replaceUserLang($this->name))); + } + } + + // Check if a strict value is not one of the specified options. + if ($this->is_strict === 'Y' && $value) + { + if ($this->canHaveOptions()) + { + $options = $this->getOptions(); + foreach ($values as $v) + { + if (!in_array($v, $options)) + { + return new BaseObject(-1, sprintf(lang('common.filter.equalto'), Context::replaceUserLang($this->name))); + } + } + } + elseif ($this->isArrayType()) + { + if (!$is_array) + { + return new BaseObject(-1, sprintf(lang('common.filter.equalto'), Context::replaceUserLang($this->name))); + } + } + } + + return null; + } + /** * Get the next temporary ID. *