dbmsv4 init...5

This commit is contained in:
최준흠 2026-02-06 17:59:47 +09:00
parent 187bd2af10
commit 509cd0e999

View File

@ -132,6 +132,21 @@ abstract class CommonForm
* @param array $formDatas 검증할 데이터 * @param array $formDatas 검증할 데이터
* @throws RuntimeException * @throws RuntimeException
*/ */
protected function sanitizeFormDatas($data, string $path = '')
{
if (!is_array($data))
return $data;
foreach ($data as $k => $v) {
if (is_array($v)) {
$data[$k] = $this->sanitizeFormDatas($v, ($path !== '' ? "$path.$k" : (string) $k));
} elseif ($v === null) {
$data[$k] = '';
}
}
return $data;
}
final public function validate(array &$formDatas): void final public function validate(array &$formDatas): void
{ {
if ($this->_validation === null) { if ($this->_validation === null) {
@ -139,35 +154,11 @@ abstract class CommonForm
} }
try { try {
$dynamicRules = []; //목적은 검증 전에 데이터 형태를 안전하게 정리
$formDatas = $this->sanitizeFormDatas($formDatas);
// ✅ 0. 데이터 정제: "배열은 절대 문자열 캐스팅 금지"
// - null만 ''로 바꿔 trim(null) 방지
// - 배열은 그대로 유지 (role 같은 is_array 규칙 깨지지 않게)
$sanitize = function (&$data, $path = '') use (&$sanitize) {
foreach ($data as $key => &$value) {
$currentField = $path ? "{$path}.{$key}" : $key;
if (is_array($value)) {
$sanitize($value, $currentField);
continue;
}
if ($value === null) {
$data[$key] = '';
continue;
}
// 숫자/불리언은 그대로 둬도 됨. (trim은 문자열 규칙에서만 적용됨)
// 굳이 문자열로 바꾸고 싶으면 여기서 처리 가능하지만, 배열에는 절대 적용하지 말 것.
}
};
$sanitize($formDatas);
// 1. 필드 라벨/규칙 // 1. 필드 라벨/규칙
$formFields = $this->getFormFields(); $formFields = $this->getFormFields();
$formRules = $this->getFormRules(); $formRules = $this->getFormRules();
if (empty($formRules)) { if (empty($formRules)) {
throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 지정된 Form RULE이 없습니다."); throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 지정된 Form RULE이 없습니다.");
} }
@ -184,9 +175,7 @@ abstract class CommonForm
$formDatas[$parent] = []; $formDatas[$parent] = [];
} elseif (is_string($formDatas[$parent])) { } elseif (is_string($formDatas[$parent])) {
// 혹시 문자열로 들어오면 CSV → 배열 복원 (공통 방어막) // 혹시 문자열로 들어오면 CSV → 배열 복원 (공통 방어막)
$formDatas[$parent] = ($formDatas[$parent] === '') $formDatas[$parent] = ($formDatas[$parent] === '') ? [] : explode(DEFAULTS["DELIMITER_COMMA"], $formDatas[$parent]);
? []
: explode(DEFAULTS["DELIMITER_COMMA"], $formDatas[$parent]);
} elseif (!is_array($formDatas[$parent])) { } elseif (!is_array($formDatas[$parent])) {
$formDatas[$parent] = []; $formDatas[$parent] = [];
} }
@ -209,6 +198,7 @@ abstract class CommonForm
} }
// 4. rules 설정 // 4. rules 설정
$dynamicRules = [];
$dynamicRules[$field] = [ $dynamicRules[$field] = [
'label' => $label, 'label' => $label,
'rules' => $rule 'rules' => $rule
@ -225,9 +215,7 @@ abstract class CommonForm
throw new RuntimeException("유효성 검사 규칙 준비 중 오류 발생 (필드: {$field}): " . $e->getMessage()); throw new RuntimeException("유효성 검사 규칙 준비 중 오류 발생 (필드: {$field}): " . $e->getMessage());
} }
} }
$this->_validation->setRules($dynamicRules); $this->_validation->setRules($dynamicRules);
try { try {
if (!$this->_validation->run($formDatas)) { if (!$this->_validation->run($formDatas)) {
$errors = $this->_validation->getErrors(); $errors = $this->_validation->getErrors();