Validate required and strict extra vars on document insert and update

This commit is contained in:
Kijin Sung 2024-10-08 22:17:00 +09:00
parent f81c296a90
commit b438666ea7
2 changed files with 102 additions and 5 deletions

View file

@ -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);
}
}

View file

@ -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.
*