I have a context where a conference can have 1 or more registration types and the user can do a registration at a conference in 1 or more registration types of the conference.
So for example, the conference with id 1 has two registration types associated, the registration type "general" with id 1 and "plus" with id 2. The registration type "general" has 6 custom questions associated with it, the registration type "plus" don't have any custom question associated with it.
When a user is doing a registration in a conference and select quantity "2" for the registration type "plus" and click "Next" the user goes to the registration page. In this page, there is the registration form and the user only needs to enter his name and surname and click "Register". The $request->all shows like below, the participant's array stores the name, surname, and registration type of each participant:
array:4 [▼ "participant" => array:2 [▼ 1 => array:3 [▼ "name" => "John" "surname" => "W" "rtypes" => "2" ] 2 => array:3 [▼ "name" => "Jake" "surname" => "K" "rtypes" => "2" ] ] ] And it works fine all info is correctly stored using the storeRegistrationInfo() method below.
Doubt:
But if the user is doing a registration and select quantity "2" for the registration type "general" and click "Next", the registration type "general" has 6 custom questions associated with it. So in the registration form the user needs to enter his name and surname but the user also needs to answer the 6 required custom questions. My doubt is how to store for each participant the answers to the custom questions, do you know how to properly achieve that? Because as it is, after the user enter the name, surname and the answer to the 6 custom questions and click "Next", the $request->all() dont shows the correct info, it shows like:
array:4 [▼ "participant" => array:4 [▼ 1 => array:5 [▼ "name" => "John" "surname" => "W" "answer" => "test.jpg" "question_id" => "6" "rtypes" => "1" ] " 1" => array:1 [▼ "answer" => "option 1" ] 2 => array:5 [▼ "name" => "Jake" "surname" => "K" "answer" => "test.jpg" "question_id" => "6" "rtypes" => "1" ] " 2" => array:1 [▼ "answer" => "option 2" ] ] "participant_question_required" => array:12 [▼ 0 => "1" 1 => "1" 2 => "1" 3 => "1" 4 => "1" 5 => "1" 6 => "1" 7 => "1" 8 => "1" 9 => "1" 10 => "1" 11 => "1" ] ] And so instead of the storeRegistration() stores all necessary info it appear always 3 validation errors:
The field name is mandatory. The field surname is mandatory. Please answer the custom questions. Do you know why? The user anwers all this fields but it appear that validation errors.
Below there is the RegistrationController storeRegistration() method that stores the registration info:
class RegistrationController extends Controller { public function storeRegistration(Request $request, $id, $slug = null) { $rules = [ 'participant.*.name' => 'required', 'participant.*.surname' => 'required', ]; $customMessages = [ 'participant.*.name.required' => 'The field name is required.', 'participant.*.surname.required' => 'The field surname is required.' ]; if (isset($request->participant_question_required)) { foreach ($request->participant_question_required as $key => $value) { $rule = 'string|max:255'; // if this was required, ie 1, prepend "required|" to the rule if ($value) { $rule = 'required|' . $rule; } // add the individual rule for this array key to the $rules array $rules["participant_question.{$key}"] = $rule; $customMessages += [ 'participant_question.*.required' => 'Please answer to the required custom questions.', ]; } } $this->validate($request, $rules, $customMessages); $user = Auth::user(); // insert registration in DB $registration = Registration::create([ 'conference_id' => $id, 'user_that_did_the_registration' => $user->id, 'status' => ($total > 0) ? 'I' : 'C' ]); // list of all participants (a registration can have multiple participants) $participants_list = $request->get('participant'); // add all participants to DB foreach ($participants_list as $participant) { $name = $participant['name']; $surname = $participant['surname']; $participant_result = Participant::create([ 'name' => $name, 'surname' => $surname, 'registration_id' => $registration->id, 'registration_type_id' => $participant['rtypes'] ]); // store all answers to the custom questions in DB if (isset($participant['question_id'])) { $answer = Answer::create([ 'question_id' => $participant['question_id'], 'participant_id' => $participant_result->id, 'answer' => $participant['answer'], ]); } } Session::flash('registration_success', 'You are registered in the conference'); return redirect(route('user.index', ['user' => Auth::id()]) . '#myTickets'); } } Registration Form:
<form method="post" action="https://proj.test/conference/1/conference-test/registration/storeRegistration"> <h6>Participant - 1 - general</h6> <div class="form-group font-size-sm"> <label for="namegeneral_1" class="text-gray">Name</label> <input type="text" required id="namegeneral_1" name="participant[name]" class="form-control" value=""> </div> <div class="form-group font-size-sm"> <label for="surnamegeneral_1" class="text-gray">Surname</label> <input type="text" required id="surnamegeneral_1" class="form-control" name="participant[surname]" value=""> </div> <div class="form-group"> <label for="participant_question">Input text custom question</label> <input type='text' name='participant[1][answer]' class='form-control' required> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="1" name="participant[1][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Long text custom question</label> <textarea name='participant[1][answer]' class='form-control' rows='3' required></textarea> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="2" name="participant[1][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Checkbox custom question</label> <div class='checkbox-group required'> <div class='form-check'> <input type='checkbox' name='participant[1][answer]' value='check1' class='form-check-input'> <label class='form-check-label' for='exampleCheck1'>check1</label> </div> <div class='form-check'> <input type='checkbox' name='participant[1][answer]' value='check2' class='form-check-input'> <label class='form-check-label' for='exampleCheck1'>check2</label> </div> </div> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="3" name="participant[1][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Radio button custom question</label> <div class='form-check'> <input type='radio' name='participant[1][answer]' value='radio button 1' class='form-check-input' required> <label class="form-check-label" for="exampleCheck1">radio button 1</label></div> <div class='form-check'> <input type='radio' name='participant[1][answer]' value='radio button 2' class='form-check-input' required> <label class="form-check-label" for="exampleCheck1">radio button 2</label></div> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="4" name="participant[1][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Select menu custom question</label> <select name='participant[ 1][answer]' class='form-control' required> <option value='option 1'>option 1</option> <option value='option 2'>option 2</option> </select> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="5" name="participant[1][question_id]"/> </div> <div class="form-group"> <label for="participant_question">File custom question</label> <input type='file' name='participant[1][answer]' class='form-control' required> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="6" name="participant[1][question_id]"/> </div> <input type="hidden" name="participant[1][rtypes]" value="1"/> <h6>Participant - 2 - general</h6> <div class="form-group font-size-sm"> <label for="namegeneral_2" class="text-gray">Name</label> <input type="text" required id="namegeneral_2" name="participant[name]" class="form-control" value=""> </div> <div class="form-group font-size-sm"> <label for="surnamegeneral_2" class="text-gray">Surname</label> <input type="text" required id="surnamegeneral_2" class="form-control" name="participant[surname]" value=""> </div> <div class="form-group"> <label for="participant_question">Input type text custom question</label> <input type='text' name='participant[2][answer]' class='form-control' required> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="1" name="participant[2][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Long text custom question</label> <textarea name='participant[2][answer]' class='form-control' rows='3' required></textarea> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="2" name="participant[2][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Checkbox custom question</label> <div class='checkbox-group required'> <div class='form-check'> <input type='checkbox' name='participant[2][answer]' value='check1' class='form-check-input'> <label class='form-check-label' for='exampleCheck1'>check1</label> </div> <div class='form-check'> <input type='checkbox' name='participant[2][answer]' value='check2' class='form-check-input'> <label class='form-check-label' for='exampleCheck1'>check2</label> </div> </div> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="3" name="participant[2][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Radio button custom question</label> <div class='form-check'> <input type='radio' name='participant[2][answer]' value='radio button 1' class='form-check-input' required> <label class="form-check-label" for="exampleCheck1">radio button 1</label></div> <div class='form-check'> <input type='radio' name='participant[2][answer]' value='radio button 2' class='form-check-input' required> <label class="form-check-label" for="exampleCheck1">radio button 2</label></div> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="4" name="participant[2][question_id]"/> </div> <div class="form-group"> <label for="participant_question">Select menu custom question</label> <select name='participant[ 2][answer]' class='form-control' required> <option value='option 1'>option 1</option> <option value='option 2'>option 2</option> </select> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="5" name="participant[2][question_id]"/> </div> <div class="form-group"> <label for="participant_question">File custom question</label> <input type='file' name='participant[2][answer]' class='form-control' required> <input type="hidden" name="participant_question_required[]" value="1"> <input type="hidden" value="6" name="participant[2][question_id]"/> </div> <input type="hidden" name="participant[2][rtypes]" value="1"/> <input type="submit" class="btn btn-primary" value="Register"/> </form> 2 Answers
Answers 1
Update your registration form using the following form fields :
change
participant[name]toparticipant_name[user_index]. [eg.participan_name[1]], whereuser_indexis the index of quantity.change
participant[surname]toparticipant_surname[user_index]. [eg.participan_surname[1]], whereuser_indexis the index of quantity.change
participant[1][answer]toanswer[user_index][question_id]. [eg.participan_surname[1][5]], whereuser_indexis the index of quantity andquestion_idis the primary_key of the question.change
participant_question_required[]toparticipant_question_required[question_id]. [eg.participant_question_required[5]], wherequestion_idis the primary_key of the question. set it's value to1if required else set to0.You may ignore
participant[1][question_id].[In this method this field is not required]
Update controller- validator using the following method:
$rules = [ 'participant_name.*' => 'required', 'participant_surname.*' => 'required', ]; $customMessages = [ 'participant_name.*.required' => 'The field name is required.', 'participant_surname.*.required' => 'The field surname is required.' ]; //second section if (isset($request->participant_question_required)) { foreach ($request->participant_question_required as $questionId => $value) { $answerRule = 'string|max:255'; // if this was required, ie 1, prepend "required|" to the rule if ($value == 1) { // add the individual rule for this array key to the $rules array $rules['answer.*.$questionId'] = 'required|' . $answerRule; $customMessages += [ 'answer.*.$questionId'' => [ 'unique' => 'Please answer to the required custom questions.', ], ]; } } } $this->validate($request, $rules, $customMessages); Hope it helps..
Answers 2
Change name="participant[{{$counter}}][surname]" to name="participant[surname]" and Change name="participant[{{$counter}}][name]" to name="participant[name]" and validations will work.
Understand what you are sending
If you recheck your $request then you will see that $participent is multidimentional array and only first array is having name index but second array does not contain name so it is throwing error back.
validation participent.*.name want only one dimensional array of names like this:
$participent[0] = 'name1' $participent[1] = 'name2' $participent[2] = 'name3' $participent[3] = 'name4' that means every index of $participent array must have name index, but you are sending it wrong as below.
$participent[0] = [name=> somename, ...] $participent[1] = [question=>'abc', answer => 'abc'] So second index of $participent array does not have name index and your validations wants it.
0 comments:
Post a Comment