*/ public function rules() : array { return [ 'document_id' => 'required|integer|exists:documents,id', 'kode' => 'required|string|unique:document_details,kode', 'document_type_id' => 'required|string|exists:document_type,document_type_id', 'special_code_id' => 'required|integer|exists:special_codes,id', 'nama_nasabah' => 'nullable|string', 'no_rekening' => 'nullable|string', 'no_cif' => 'nullable|string', 'group' => 'nullable|string', 'tanggal_dokumen' => 'nullable|date', 'tanggal_upload' => 'nullable|date', 'nomor_dokumen' => 'nullable|string', 'perihal' => 'nullable|string', 'kode_cabang' => 'nullable|string', 'jumlah_halaman' => 'nullable|integer', 'custom_field_1' => 'nullable|string', 'custom_field_2' => 'nullable|string', 'custom_field_3' => 'nullable|string', 'custom_field_4' => 'nullable|string', 'status' => 'nullable|string', 'file' => 'nullable|string', 'no_urut' => 'nullable|integer', 'keterangan' => 'nullable|string', 'status' => 'nullable|string', 'kategori' => 'nullable|string', ]; } /** * Configure the validator instance. */ public function withValidator(Validator $validator) : void { $validator->after(function (Validator $validator) { if ($validator->errors()->any()) { $error = json_decode($validator->errors()->toJson(), true); foreach ($error as $key => $value) { flash($value[0]); } return redirect()->route('document.index')->with('error', 'Document created failed.'); } }); } protected function failedValidation(Validator|\Illuminate\Contracts\Validation\Validator $validator) : JsonResponse { $errors = (new ValidationException($validator))->errors(); throw new HttpResponseException(response()->json([ 'success' => false, 'errors' => $errors, 'messages' => 'Document created failed.' ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); } protected function prepareForValidation() : void { $document = Document::find($this->document_id); $special_code = SpecialCode::find($this->special_code_id); $prefix = $document->kode; $suffix = $special_code->kode; $kode = IdGenerator::generate([ 'table' => 'document_details', 'field' => 'kode', 'length' => 13, 'prefix' => $prefix ]); $this->merge([ 'kode' => $kode . $suffix, 'no_urut' => substr($kode, -3, 3) ]); } }