From d4ad6e9a470c52943f8baac5ca082f1eda24b144 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 25 Jul 2024 02:46:46 +0530 Subject: [PATCH] Fixed bug on the form responding page --- app/Http/Controllers/FormController.php | 55 ++++----- public/css/app.css | 4 + resources/views/forms/edit.blade.php | 13 ++- resources/views/layouts/app.blade.php | 3 +- resources/views/responses/showForm.blade.php | 117 ++++++++++--------- 5 files changed, 101 insertions(+), 91 deletions(-) diff --git a/app/Http/Controllers/FormController.php b/app/Http/Controllers/FormController.php index 763126d..a420012 100644 --- a/app/Http/Controllers/FormController.php +++ b/app/Http/Controllers/FormController.php @@ -104,41 +104,42 @@ Contact us at (123) 456-7890 or no_reply@example.com public function store(Request $request) { + $validatedData = $request->validate([ + 'title' => 'required|string|max:255', + 'description' => 'nullable|string', + 'questions' => 'required|array', + 'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text', + 'questions.*.text' => 'required|string', + 'questions.*.options' => 'nullable|array', + 'questions.*.required' => 'boolean', + ]); + + DB::beginTransaction(); + try { - $validatedData = $request->validate([ - 'title' => 'required|string|max:255', - 'description' => 'nullable|string', - 'questions' => 'required|array', - 'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text', - 'questions.*.text' => 'required|string', - 'questions.*.options' => 'nullable|array', - 'questions.*.required' => 'nullable|boolean', + $form = Form::create([ + 'title' => $validatedData['title'], + 'description' => $validatedData['description'], + 'user_id' => Auth::id(), ]); - - $form = new Form(); - $form->title = $validatedData['title']; - $form->description = $validatedData['description']; - $form->is_published = $request->input('is_published', false); - $form->user_id = Auth::id(); - $form->save(); - foreach ($validatedData['questions'] as $questionData) { - $question = new Question(); - $question->form_id = $form->id; - $question->type = $questionData['type']; - $question->question_text = $questionData['text']; - $question->options = isset($questionData['options']) ? json_encode($questionData['options']) : null; - $question->required = isset($questionData['required']) ? $questionData['required'] : false; + $question = new Question([ + 'form_id' => $form->id, + 'type' => $questionData['type'], + 'question_text' => $questionData['text'], + 'options' => json_encode($questionData['options'] ?? []), + 'required' => $questionData['required'], + ]); $question->save(); } - - - return response()->json(['success' => true, 'form_id' => $form->id]); + DB::commit(); + return response()->json(['success' => true, 'message' => 'Form saved successfully.']); } catch (\Exception $e) { - Log::error('Error saving form: ' . $e->getMessage(), ['exception' => $e]); - return response()->json(['success' => false, 'message' => 'Error saving form'], 500); + DB::rollBack(); + Log::error('Error saving form: ' . $e->getMessage()); + return response()->json(['success' => false, 'message' => 'Failed to save form.']); } } diff --git a/public/css/app.css b/public/css/app.css index e801b13..39f0af8 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -255,3 +255,7 @@ input:focus, textarea:focus, select:focus{ color: black; height: 20px; } + +.active-question { + border-left: 4px solid blue; +} diff --git a/resources/views/forms/edit.blade.php b/resources/views/forms/edit.blade.php index 1322937..630ac1a 100644 --- a/resources/views/forms/edit.blade.php +++ b/resources/views/forms/edit.blade.php @@ -89,7 +89,9 @@ @endforeach @endif - + +
+ @@ -112,7 +114,7 @@ diff --git a/resources/views/responses/showForm.blade.php b/resources/views/responses/showForm.blade.php index 4655392..87c8cd6 100644 --- a/resources/views/responses/showForm.blade.php +++ b/resources/views/responses/showForm.blade.php @@ -52,72 +52,73 @@
@endsection