Fixed Response Dropdown bug

This commit is contained in:
Yash 2024-07-15 23:31:10 +05:30
parent 8658ca5ca1
commit 03da2ae482
4 changed files with 36 additions and 31 deletions

View File

@ -1,7 +1,7 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid;
use App\Models\Form;
use App\Models\Response;
@ -35,6 +35,17 @@ class ResponseController extends Controller
return view('responses.viewResponse', compact('form', 'responses', 'questions'));
}
public function viewResponses(Form $form)
{
// Get all responses for the form, grouped by response_id
$responses = Response::where('form_id', $form->id)
->orderBy('submitted_at', 'desc')
->get()
->groupBy('response_id');
return view('responses.viewResponses', compact('form', 'responses'));
}
public function showForm(Form $form)
{
$questions = $form->questions;
@ -44,12 +55,16 @@ class ResponseController extends Controller
public function submitForm(Request $request, Form $form)
{
Log::info($request->all()); // Log the entire request data for debugging
// Validate and process form submission
$validatedData = $request->validate([
'answers' => 'required|array',
'answers.*' => 'required',
]);
Log::info($validatedData); // Log the validated data for debugging
// Generate a UUID for response_id
$responseId = Uuid::uuid4()->toString();
@ -67,16 +82,5 @@ class ResponseController extends Controller
return redirect()->route('responses.showForm', $form)
->with('success', 'Response submitted successfully.');
}
// View responses for the form owner
public function viewResponses(Form $form)
{
// Get all responses for the form, grouped by response_id
$responses = Response::where('form_id', $form->id)
->orderBy('submitted_at', 'desc')
->get()
->groupBy('response_id');
return view('responses.viewResponses', compact('form', 'responses'));
}
}

2
package-lock.json generated
View File

@ -1,5 +1,5 @@
{
"name": "LaraForms-main",
"name": "Forms_Laravel",
"lockfileVersion": 3,
"requires": true,
"packages": {

View File

@ -72,11 +72,15 @@
</label>
@endforeach
@elseif($question->type == 'dropdown')
<select class="form-select mt-2 block w-full p-2 border border-gray-300 rounded-md bg-white shadow-sm focus:outline-none focus:ring-purple-500 focus:border-purple-500 sm:text-sm">
<select class="form-select mt-2 block w-full p-2 border border-gray-300 rounded-md bg-white shadow-sm focus:outline-none focus:ring-purple-500 focus:border-purple-500 sm:text-sm" name="answers[{{ $question->id }}]">
@foreach (json_decode($question->options) as $option)
<option value="{{ $option }}">{{ $option }}</option>
@endforeach
</select>
@elseif($question->type == 'short_answer')
<input type="text" name="answers[{{ $question->id }}]" class="form-input mt-2 block w-full p-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-purple-500 focus:border-purple-500 sm:text-sm">
@elseif($question->type == 'long_answer')
<textarea name="answers[{{ $question->id }}]" class="form-textarea mt-2 block w-full p-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-purple-500 focus:border-purple-500 sm:text-sm"></textarea>
@endif
</div>
@endforeach
@ -87,3 +91,4 @@
</form>
</div>
@endsection

View File

@ -81,9 +81,6 @@
<div class="response_detail">
<h2>Response from {{ $responses->first()->user->name ?? 'Anonymous' }} - {{ $responses->first()->submitted_at }}</h2>
{{-- Debugging output --}}
<pre>{{ print_r($questions) }}</pre>
<pre>{{ print_r($responses) }}</pre>
@foreach ($responses as $response)
@php
@ -94,23 +91,21 @@
@if ($question)
<div class="question">
<h3>{{ $question->question_text }}</h3>
@if (in_array($question->type, ['multiple_choice', 'checkbox', 'dropdown']))
@if ($question->type == 'dropdown')
<select disabled>
@foreach (json_decode($question->options) as $option)
<option {{ ($option == $response->answers) ? 'selected' : '' }}>
<option {{ ($option == $decodedAnswers) ? 'selected' : '' }}>
{{ $option }}
</option>
@endforeach
</select>
@else
@elseif (in_array($question->type, ['multiple_choice', 'checkbox']))
@foreach (json_decode($question->options) as $option)
<p>
<input type="radio" disabled {{ in_array($option, (array)$decodedAnswers) ? 'checked' : '' }}>
<input type="{{ $question->type == 'checkbox' ? 'checkbox' : 'radio' }}" disabled {{ in_array($option, (array)$decodedAnswers) ? 'checked' : '' }}>
{{ $option }}
</p>
@endforeach
@endif
@else
<p>{{ is_array($decodedAnswers) ? implode(', ', $decodedAnswers) : $decodedAnswers }}</p>
@endif
@ -126,3 +121,4 @@
</html>