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 <?php
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use App\Models\Form; use App\Models\Form;
use App\Models\Response; use App\Models\Response;
@ -35,6 +35,17 @@ class ResponseController extends Controller
return view('responses.viewResponse', compact('form', 'responses', 'questions')); 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) public function showForm(Form $form)
{ {
$questions = $form->questions; $questions = $form->questions;
@ -44,12 +55,16 @@ class ResponseController extends Controller
public function submitForm(Request $request, Form $form) public function submitForm(Request $request, Form $form)
{ {
Log::info($request->all()); // Log the entire request data for debugging
// Validate and process form submission // Validate and process form submission
$validatedData = $request->validate([ $validatedData = $request->validate([
'answers' => 'required|array', 'answers' => 'required|array',
'answers.*' => 'required', 'answers.*' => 'required',
]); ]);
Log::info($validatedData); // Log the validated data for debugging
// Generate a UUID for response_id // Generate a UUID for response_id
$responseId = Uuid::uuid4()->toString(); $responseId = Uuid::uuid4()->toString();
@ -67,16 +82,5 @@ class ResponseController extends Controller
return redirect()->route('responses.showForm', $form) return redirect()->route('responses.showForm', $form)
->with('success', 'Response submitted successfully.'); ->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, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {

View File

@ -72,11 +72,15 @@
</label> </label>
@endforeach @endforeach
@elseif($question->type == 'dropdown') @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) @foreach (json_decode($question->options) as $option)
<option value="{{ $option }}">{{ $option }}</option> <option value="{{ $option }}">{{ $option }}</option>
@endforeach @endforeach
</select> </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 @endif
</div> </div>
@endforeach @endforeach
@ -87,3 +91,4 @@
</form> </form>
</div> </div>
@endsection @endsection

View File

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