'master_kabupaten_kota', 'kecamatan_id' => 'master_kecamatan', 'desa_kelurahan_id' => 'master_desa_kelurahan']; if ($profile) { $tables = ['provinsi_id' => 'master_provinsi', ...$tables]; } $available = app(WilayahKoperasiService::class)->available(); $rules = []; foreach ($tables as $field => $table) { $rules[$field] = ['bail', Rule::prohibitedIf(! $available), 'nullable', 'integer', Rule::exists($table, 'id')->whereNull('deleted_at')]; if ($profile) { $others = array_diff(array_keys($tables), [$field]); $rules[$field][] = Rule::requiredIf(fn () => collect($others)->contains(fn ($other) => $this->filled($other))); } } return $rules; } protected function wilayahAttributes(): array { return ['provinsi_id' => 'Provinsi', 'kabupaten_kota_id' => 'Kabupaten/Kota', 'kecamatan_id' => 'Kecamatan', 'desa_kelurahan_id' => 'Desa/Kelurahan']; } public function after(): array { return [function (Validator $validator) { $labels = $this->wilayahAttributes(); if ($validator->errors()->hasAny(array_keys($labels))) { return; } foreach ([ ['kabupaten_kota_id', 'provinsi_id', 'master_kabupaten_kota'], ['kecamatan_id', 'kabupaten_kota_id', 'master_kecamatan'], ['desa_kelurahan_id', 'kecamatan_id', 'master_desa_kelurahan'], ] as [$child, $parent, $table]) { if ($parent === 'provinsi_id' && ! $this instanceof StorePengurusKoperasiRequest) { continue; } if ($this->filled($child) && $this->filled($parent) && ! DB::table($table)->where('id', $this->input($child))->where($parent, $this->input($parent))->exists()) { $validator->errors()->add($child, 'Pilihan '.$labels[$child].' tidak sesuai dengan '.$labels[$parent].'.'); } } }]; } }