adamjhn
81
18
Laravel

Why the validation is not working properly?

Posted 8 months ago by adamjhn

I have the form below for a user register in a conference. The form can have custom questions, in this case, it has the custom question "Phone" and is a required question so I want to validate that in Laravel. The name and surname fields should also be required.

But the validation it´s not working properly, even if the "required" attribute is removed from the source code and the "Store Registration" button is clicked the code never enters in "`if ($validator->passes()) {...".

Do you know why the validation is not working?

storeRegistration():




     public function storeRegistration(Request $request, $id, $slug = null)
        {
            
            $rules = [];
            $messages = [];
    
            $rules["participant_name.*"] = 'required|max:255|string';
            $rules["participant_surname.*"] = 'required|max:255|string';
    
            if (isset($request->participant_question_required)) {
    
                $messages = [
                    'participant_question.*.required' => 'Fill all mandatory fields',
                ];
    
                foreach ($request->participant_question_required as $key => $value) {
                    $rule = 'string|max:255';
    
                    if ($value) {
                        $rule = 'required|' . $rule;
    
                    }
                    $rules["participant_question.{$key}"] = $rule;
    
                }
            }
            
            $validator = Validator::make($request->all(), $rules, $messages);
    
    
            if ($validator->passes()) {
    
                dd('test');
    
                $total = Session::get('total');
    
                # user object
                $user = Auth::user();
    
                $registration = Registration::create([
                    'conference_id' => $id,
                    'main_participant_id' => $user->id,
                    'status' => ($total > 0) ? 'I' : 'C',
                ]);
    
                $participants_list = $request->get('participant');
    
                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']
                    ]);
    
                    if (isset($participant['question_id'])) {
                        $answer = Answer::create([
                            'question_id' => $participant['question_id'],
                            'participant_id' => $participant_result->id,
                            'answer' => $participant['answer'],
                        ]);
                    }
                }
                return redirect(route('user.index', ['user' => Auth::id()]) . '#myTickets');
            } else {
                dd($validator->errors());
            }
        }

The "dd($validator->errors());" shows:


    MessageBag {#271 ▼
      #messages: array:2 [▼
        "participant_question.0" => array:1 [▼
          0 => "FIll all mandatory fields"
        ]
        "participant_question.1" => array:1 [▼
          0 => "Fill all mandatory fields"
        ]
      ]
      #format: ":message"
    }

Form for the user to register in a conference:


    <form method="post" id="registration_form" action="">
      <input type="hidden" name="_token" value="">        
    
      <h6 > Participant - 1 - geral</h6>    
    
      <div class="form-group font-size-sm">
        <label for="namegeral_1" class="text-gray">Name</label>
        <input type="text" id="namegeral_1" name="participant[1][name]" required="" class="form-control" value="">
      </div>
      
      <div class="form-group font-size-sm">
        <label for="surnamegeral_1" class="text-gray">Surname</label>
        <input type="text" id="surnamegeral_1" required="" class="form-control" name="participant[1][surname]" value="">
      </div>
    
      <div class="form-group">
        <label for="participant_question">Phone?</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>
    
      <input type="hidden" name="participant[1][rtypes]" value="1">
    
      <h6> Participant - 2 - geral</h6>
    
      <div class="form-group font-size-sm">
        <label for="namegeral_2" class="text-gray">Name</label>
        <input type="text" id="namegeral_2" name="participant[2][name]" required="" class="form-control" value="">
      </div>
      
      <div class="form-group font-size-sm">
        <label for="surnamegeral_2" class="text-gray">Surname</label>
        <input type="text" id="surnamegeral_2" required="" class="form-control" name="participant[2][surname]" value="">
      </div>
    
      <div class="form-group">
        <label for="participant_question">Phone?</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>
    
      <input type="hidden" name="participant[2][rtypes]" value="1">
    
    
      <h6> Participant - 3 - plus</h6>    
    
      <div class="form-group font-size-sm">
        <label for="nameplus_3" class="text-gray">Name</label>
        <input type="text" id="nameplus_3" name="participant[3][name]" required="" class="form-control" value="">
      </div>
      <div class="form-group font-size-sm">
        <label for="surnameplus_3" class="text-gray">Surname</label>
        <input type="text" id="surnameplus_3" required="" class="form-control" name="participant[3][surname]" value="">
      </div>
    
      <input type="hidden" name="participant[3][rtypes]" value="2">
      <input type="submit" class="btn btn-primary" value="Store Registration">
    </form>

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.