dbmsv4 init...5
This commit is contained in:
parent
187bd2af10
commit
509cd0e999
@ -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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user