cfmgrv4 change validation,,,

This commit is contained in:
최준흠 2025-02-14 17:19:47 +09:00
parent 5b19a6dbde
commit 2cbd5a5161
2 changed files with 19 additions and 18 deletions

View File

@ -35,17 +35,18 @@ class UserController extends AdminController
} }
return $this->service; return $this->service;
} }
protected function setValidation(string $action, string $field): void protected function setValidation(string $action, string $field, Validation $validation): Validation
{ {
switch ($field) { switch ($field) {
case 'role': case 'role':
//아래 Rule Array는 필드명.* checkbox를 사용 //아래 Rule Array는 필드명.* checkbox를 사용
$this->_validation->setRule("{$field}.*", $field, $this->getModel()->getFieldRule($action, $field)); $validation->setRule("{$field}.*", $field, $this->getModel()->getFieldRule($action, $field));
break; break;
default: default:
parent::setValidation($action, $field); $validation = parent::setValidation($action, $field, $validation);
break; break;
} }
return $validation;
} }
private function init(string $action, array $fields = []): void private function init(string $action, array $fields = []): void
{ {

View File

@ -14,7 +14,6 @@ use Psr\Log\LoggerInterface;
abstract class MVController extends CommonController abstract class MVController extends CommonController
{ {
protected $_validation = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
@ -23,20 +22,22 @@ abstract class MVController extends CommonController
abstract protected function getModel(): mixed; abstract protected function getModel(): mixed;
abstract protected function getService(): mixed; abstract protected function getService(): mixed;
//Field별 Form Rule용 //Field별 Form Rule용
protected function setValidation(string $action, string $field): void protected function setValidation(string $action, string $field, Validation $validation): Validation
{ {
switch ($field) { switch ($field) {
default: default:
$this->_validation->setRule($field, $field, $this->getModel()->getFieldRule($action, $field)); $validation->setRule($field, $field, $this->getModel()->getFieldRule($action, $field));
break; break;
} }
return $validation;
} }
final protected function setValidations(string $action, array $fields): void final protected function getValidation(string $action, array $fields): Validation
{ {
$this->_validation = service('validation'); $validation = service('validation');
foreach ($fields as $field) { foreach ($fields as $field) {
$this->setValidation($action, $field); $validation = $this->setValidation($action, $field, $validation);
} }
return $validation;
} }
//Field별 Form Option용 //Field별 Form Option용
protected function getFormFieldOption(string $field, array $options): array protected function getFormFieldOption(string $field, array $options): array
@ -80,14 +81,14 @@ abstract class MVController extends CommonController
protected function create_validate(string $action, array $fields): array protected function create_validate(string $action, array $fields): array
{ {
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요 //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요
$this->setValidations($action, $fields); $validation = $this->getValidation($action, $fields);
if (!$this->_validation->withRequest($this->request)->run()) { if (!$validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->getService()->class_name} 작업 데이터 검증 오류발생\n" . implode( throw new \Exception("{$this->getService()->class_name} 작업 데이터 검증 오류발생\n" . implode(
"\n", "\n",
$this->_validation->getErrors() $validation->getErrors()
)); ));
} }
return $this->_validation->getValidated(); return $validation->getValidated();
} }
protected function create_process(): void protected function create_process(): void
{ {
@ -143,15 +144,14 @@ abstract class MVController extends CommonController
final protected function modify_validate(string $action, array $fields): array final protected function modify_validate(string $action, array $fields): array
{ {
//변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요 //변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
$this->setValidations($action, $fields); $validation = $this->getValidation($action, $fields);
if (!$this->_validation->withRequest($this->request)->run()) { if (!$validation->withRequest($this->request)->run()) {
throw new \Exception("{$this->getService()->class_name} 작업 데이터 검증 오류발생\n" . implode( throw new \Exception("{$this->getService()->class_name} 작업 데이터 검증 오류발생\n" . implode(
"\n", "\n",
$this->_validation->getErrors() $validation->getErrors()
)); ));
} }
// dd((array)$this->_validation); return $validation->getValidated();
return $this->_validation->getValidated();
} }
//modify,toggle,batchjob 공통사용 //modify,toggle,batchjob 공통사용
protected function modify_process(mixed $uid): void protected function modify_process(mixed $uid): void