127 lines
4.1 KiB
PHP
127 lines
4.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Requests;
|
||
|
|
|
||
|
|
use Illuminate\Foundation\Http\FormRequest;
|
||
|
|
use Shared\Models\Products\ModelParameter;
|
||
|
|
|
||
|
|
class ModelParameterRequest extends FormRequest
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Determine if the user is authorized to make this request.
|
||
|
|
*/
|
||
|
|
public function authorize(): bool
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the validation rules that apply to the request.
|
||
|
|
*/
|
||
|
|
public function rules(): array
|
||
|
|
{
|
||
|
|
$rules = [
|
||
|
|
'model_id' => 'required|integer|exists:models,id',
|
||
|
|
'name' => 'required|string|max:50|regex:/^[a-zA-Z][a-zA-Z0-9_]*$/',
|
||
|
|
'label' => 'required|string|max:100',
|
||
|
|
'type' => 'required|string|in:' . implode(',', ModelParameter::TYPES),
|
||
|
|
'unit' => 'nullable|string|max:20',
|
||
|
|
'validation_rules' => 'nullable|array',
|
||
|
|
'options' => 'nullable|array',
|
||
|
|
'default_value' => 'nullable',
|
||
|
|
'order' => 'nullable|integer|min:1',
|
||
|
|
'description' => 'nullable|string|max:500',
|
||
|
|
'is_required' => 'boolean',
|
||
|
|
'is_active' => 'boolean',
|
||
|
|
];
|
||
|
|
|
||
|
|
// 타입별 세부 검증
|
||
|
|
if ($this->input('type') === ModelParameter::TYPE_NUMBER) {
|
||
|
|
$rules['validation_rules.min'] = 'nullable|numeric';
|
||
|
|
$rules['validation_rules.max'] = 'nullable|numeric|gte:validation_rules.min';
|
||
|
|
$rules['default_value'] = 'nullable|numeric';
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->input('type') === ModelParameter::TYPE_SELECT) {
|
||
|
|
$rules['options'] = 'required|array|min:1';
|
||
|
|
$rules['options.*'] = 'required|string|max:100';
|
||
|
|
$rules['default_value'] = 'nullable|string|in_array:options';
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->input('type') === ModelParameter::TYPE_BOOLEAN) {
|
||
|
|
$rules['default_value'] = 'nullable|boolean';
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->input('type') === ModelParameter::TYPE_TEXT) {
|
||
|
|
$rules['validation_rules.max_length'] = 'nullable|integer|min:1|max:1000';
|
||
|
|
$rules['validation_rules.pattern'] = 'nullable|string|max:200';
|
||
|
|
$rules['default_value'] = 'nullable|string';
|
||
|
|
}
|
||
|
|
|
||
|
|
return $rules;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get custom messages for validator errors.
|
||
|
|
*/
|
||
|
|
public function messages(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'name.regex' => __('error.parameter_name_format'),
|
||
|
|
'validation_rules.max.gte' => __('error.max_must_be_greater_than_min'),
|
||
|
|
'options.required' => __('error.select_type_requires_options'),
|
||
|
|
'options.min' => __('error.select_type_requires_options'),
|
||
|
|
'default_value.in_array' => __('error.default_value_not_in_options'),
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Configure the validator instance.
|
||
|
|
*/
|
||
|
|
public function withValidator($validator): void
|
||
|
|
{
|
||
|
|
$validator->after(function ($validator) {
|
||
|
|
// 추가 검증 로직
|
||
|
|
$this->validateParameterNameUnique($validator);
|
||
|
|
$this->validateValidationRules($validator);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 매개변수명 중복 검증
|
||
|
|
*/
|
||
|
|
private function validateParameterNameUnique($validator): void
|
||
|
|
{
|
||
|
|
if (!$this->input('model_id') || !$this->input('name')) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$query = ModelParameter::where('model_id', $this->input('model_id'))
|
||
|
|
->where('name', $this->input('name'));
|
||
|
|
|
||
|
|
// 수정 시 자기 자신 제외
|
||
|
|
if ($this->route('id')) {
|
||
|
|
$query->where('id', '!=', $this->route('id'));
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($query->exists()) {
|
||
|
|
$validator->errors()->add('name', __('error.parameter_name_duplicate'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 검증 규칙 유효성 검증
|
||
|
|
*/
|
||
|
|
private function validateValidationRules($validator): void
|
||
|
|
{
|
||
|
|
$type = $this->input('type');
|
||
|
|
$validationRules = $this->input('validation_rules', []);
|
||
|
|
|
||
|
|
if ($type === ModelParameter::TYPE_TEXT && isset($validationRules['pattern'])) {
|
||
|
|
// 정규식 패턴 검증
|
||
|
|
if (@preg_match($validationRules['pattern'], '') === false) {
|
||
|
|
$validator->errors()->add('validation_rules.pattern', __('error.invalid_regex_pattern'));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|