solve dashboard indikator pertanian

This commit is contained in:
2026-08-13 13:07:03 +07:00
parent ba7520a23c
commit b855a857b2
3 changed files with 114 additions and 47 deletions
@@ -55,19 +55,7 @@ class FormBasisDataController extends Controller
], 404);
}
$validated = $request->validate([
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
'tipe' => ['required', Rule::enum(LayoutForm::class)],
'column_width' => [
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
'nullable', 'integer', 'min:1', 'max:12'],
'kolom_id' => [
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
'section_label' => [
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
'nullable', 'string'],
]);
$validated = $this->validateFormPayload($request, $tabelId);
FormBasisData::create([...$validated,
'tabel_id' => $tabelId,
@@ -115,19 +103,7 @@ class FormBasisDataController extends Controller
], 404);
}
$validated = $request->validate([
'parent_id' => ['nullable', 'exists:ekosistem_basis_data_form,id'],
'tipe' => ['required', Rule::enum(LayoutForm::class)],
'column_width' => [
Rule::requiredIf($request->tipe === LayoutForm::COLUMN->value),
'nullable', 'integer', 'min:1', 'max:12'],
'kolom_id' => [
Rule::requiredIf($request->tipe === LayoutForm::VALUE->value),
'nullable', 'exists:ekosistem_basis_data_kolom,id'],
'section_label' => [
Rule::requiredIf($request->tipe === LayoutForm::SECTION->value),
'nullable', 'string'],
]);
$validated = $this->validateFormPayload($request, $tabelId, $form);
$form->update([...$validated,
'updated_by' => auth()->user()->id
@@ -293,4 +269,88 @@ class FormBasisDataController extends Controller
'message' => 'Formulir basis data berhasil diperbarui.'
], 200);
}
private function validateFormPayload(Request $request, string $tabelId, ?FormBasisData $form = null): array
{
$tipe = $request->input('tipe');
$needsParent = in_array($tipe, [LayoutForm::COLUMN->value, LayoutForm::VALUE->value], true);
$mustBeRoot = in_array($tipe, [LayoutForm::ROW->value, LayoutForm::SECTION->value, LayoutForm::HR_LINE->value], true);
$validator = Validator::make($request->all(), [
'parent_id' => [
Rule::requiredIf($needsParent),
Rule::prohibitedIf($mustBeRoot),
'nullable',
Rule::exists('ekosistem_basis_data_form', 'id')
->where(fn ($query) => $query
->where('tabel_id', $tabelId)
->whereNull('deleted_at')),
],
'tipe' => ['required', Rule::enum(LayoutForm::class)],
'column_width' => [
Rule::requiredIf($tipe === LayoutForm::COLUMN->value),
'nullable', 'integer', 'min:1', 'max:12'
],
'kolom_id' => [
Rule::requiredIf($tipe === LayoutForm::VALUE->value),
'nullable',
Rule::exists('ekosistem_basis_data_kolom', 'id')
->where(fn ($query) => $query
->where('tabel_id', $tabelId)
->whereNull('deleted_at')),
],
'section_label' => [
Rule::requiredIf($tipe === LayoutForm::SECTION->value),
'nullable', 'string'
],
]);
$validator->after(function ($validator) use ($request, $tabelId, $form) {
if ($validator->errors()->isNotEmpty() || !$request->filled('parent_id')) {
return;
}
if ($form && (int) $request->input('parent_id') === (int) $form->id) {
$validator->errors()->add('parent_id', 'Induk formulir tidak boleh menunjuk ke dirinya sendiri.');
return;
}
$layout = LayoutForm::from($request->input('tipe'));
$expectedParent = match ($layout) {
LayoutForm::COLUMN => LayoutForm::ROW,
LayoutForm::VALUE => LayoutForm::COLUMN,
default => null,
};
if (!$expectedParent) {
return;
}
$parent = FormBasisData::where('tabel_id', $tabelId)->find($request->input('parent_id'));
if (!$parent || $parent->tipe !== $expectedParent) {
$validator->errors()->add('parent_id', 'Induk formulir tidak sesuai dengan tipe formulir.');
}
});
$validated = $validator->validate();
$layout = LayoutForm::from($validated['tipe']);
if (in_array($layout, [LayoutForm::ROW, LayoutForm::SECTION, LayoutForm::HR_LINE], true)) {
$validated['parent_id'] = null;
}
if ($layout !== LayoutForm::COLUMN) {
$validated['column_width'] = null;
}
if ($layout !== LayoutForm::VALUE) {
$validated['kolom_id'] = null;
}
if ($layout !== LayoutForm::SECTION) {
$validated['section_label'] = null;
}
return $validated;
}
}