*/ public function rules() : array { return [ 'kode' => 'required|string|max:2|min:2|unique:special_codes,kode', 'name' => 'required|string|max:50', 'description' => 'nullable|string|max:255' ]; } /** * 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('special-code.index')->with('error', 'Special Code 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' => 'Special Code created failed.' ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); } }