*/ public function rules() : array { return [ 'name' => 'required|max:100|unique:roles,name', 'guard_name' => 'nullable|max:100|string' ]; } /** * Configure the validator instance. */ public function withValidator(Validator $validator) : void { $validator->after(function (Validator $validator) { if ($validator->errors()->any()) { $errors = json_decode($validator->errors()->toJson(), true); foreach ($errors as $key => $value) { flash($value[0]); } return redirect()->route('user.roles.index')->with('error', 'Role 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' => 'Role created failed.' ], JsonResponse::HTTP_UNPROCESSABLE_ENTITY)); } }