trafficmonitor/app/Forms/Auth/LocalForm.php
2025-11-07 17:44:05 +09:00

61 lines
1.9 KiB
PHP

<?php
namespace App\Forms\Auth;
use App\Forms\CommonForm;
class LocalForm extends CommonForm
{
public function __construct()
{
parent::__construct();
}
public function getFormFields(string $action, ?array $fields = null): array
{
return $fields ?? ["id", "passwd"];
}
public function getFormFilters(string $action, ?array $fields = null): array
{
return $fields ?? [];
}
public function getBatchjobButtons(string $action = 'index', ?array $buttions = null): array
{
return $buttions ?? [];
}
public function getFormRule(string $action, string $field, array $rules = []): array
{
// 부모 클래스의 기본 규칙을 먼저 가져옵니다.
$rules = parent::getFormRule($action, $field, $rules);
switch ($field) {
case "id":
// 💡 배열을 사용하여 규칙 목록을 안전하게 만듭니다.
$ruleList = [
"required",
"trim",
"min_length[4]",
"max_length[20]",
];
// 'create' 액션에만 is_unique 규칙을 추가합니다.
if (in_array($action, ["create", "create_form"])) {
// $this->getAttribute('table') 대신 $this->model->getTable()를 사용하거나,
// 해당 Form 클래스가 $this->model을 가지고 있다면 그것을 사용해야 합니다.
$ruleList[] = "is_unique[{$this->getAttribute('table')}.{$field}]";
}
// 배열 요소를 '|'로 안전하게 연결하여 최종 Rule 문자열을 생성합니다.
$rules[$field] = implode('|', $ruleList);
break;
case "passwd":
$rules[$field] = in_array($action, ["create", "create_form"])
? "required|trim|string"
: "permit_empty|trim|string";
break;
default:
$rules = parent::getFormRule($action, $field, $rules);
break;
}
return $rules;
}
}