From 509cd0e9991cf5026caea6162991fd711a251677 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 6 Feb 2026 17:59:47 +0900 Subject: [PATCH] dbmsv4 init...5 --- app/Forms/CommonForm.php | 50 +++++++++++++++------------------------- 1 file changed, 19 insertions(+), 31 deletions(-) diff --git a/app/Forms/CommonForm.php b/app/Forms/CommonForm.php index 2569636..07c561d 100644 --- a/app/Forms/CommonForm.php +++ b/app/Forms/CommonForm.php @@ -132,6 +132,21 @@ abstract class CommonForm * @param array $formDatas 검증할 데이터 * @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 { if ($this->_validation === null) { @@ -139,35 +154,11 @@ abstract class CommonForm } try { - $dynamicRules = []; - - // ✅ 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); - + //목적은 검증 전에 데이터 형태를 안전하게 정리 + $formDatas = $this->sanitizeFormDatas($formDatas); // 1. 필드 라벨/규칙 $formFields = $this->getFormFields(); $formRules = $this->getFormRules(); - if (empty($formRules)) { throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: 지정된 Form RULE이 없습니다."); } @@ -184,9 +175,7 @@ abstract class CommonForm $formDatas[$parent] = []; } elseif (is_string($formDatas[$parent])) { // 혹시 문자열로 들어오면 CSV → 배열 복원 (공통 방어막) - $formDatas[$parent] = ($formDatas[$parent] === '') - ? [] - : explode(DEFAULTS["DELIMITER_COMMA"], $formDatas[$parent]); + $formDatas[$parent] = ($formDatas[$parent] === '') ? [] : explode(DEFAULTS["DELIMITER_COMMA"], $formDatas[$parent]); } elseif (!is_array($formDatas[$parent])) { $formDatas[$parent] = []; } @@ -209,6 +198,7 @@ abstract class CommonForm } // 4. rules 설정 + $dynamicRules = []; $dynamicRules[$field] = [ 'label' => $label, 'rules' => $rule @@ -225,9 +215,7 @@ abstract class CommonForm throw new RuntimeException("유효성 검사 규칙 준비 중 오류 발생 (필드: {$field}): " . $e->getMessage()); } } - $this->_validation->setRules($dynamicRules); - try { if (!$this->_validation->run($formDatas)) { $errors = $this->_validation->getErrors();