Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adamjhn's avatar

Why the code dont enters in the “if ($validator->passes()) {…}”? (the issue is only when there are custom questions, that is when the code enters in "if (isset($request->participant_question_required)) {..}"

I have a form in the registration.blade.php for a user to enter some info to do a registration in a conference.

In the form there is this part below, that shows for each registration type selected by the user the custom questions associated with that registration type type for the user to answer. And adds the "required" attribute if in the registration_type_questions pivot table the question has the "required" column as "1".

The issue is that if there are custom questions that are required I want to validate that also in laravel. So I have the code below in the storeRegistration() that shows the message "Fill all mandatory fields" if a custom question that is require was not answered. But even if the user fill all required custom questions the code never entes in the if "if ($validator->passes()) {...}". Do you know why?

Part of the form that shows for each selected registration type the custom questions associated with it:

    <form method="post" action="{{route('conferences.storeRegistration', ['id' => $id, 'slug' => $slug])}}">
    ...
    @if ($allParticipants == 0)
        @foreach($selectedRtype['questions'] as $customQuestion)
            <div class="form-group">
                <label for="participant_question">{{$customQuestion->question}}</label>
                @if($customQuestion->hasOptions() && in_array($customQuestion->type, ['checkbox', 'radio_btn', 'select_menu']))
                    {!! $customQuestion->getHtmlInput(
                        $customQuestion->name,
                        $customQuestion->options,
                        ($customQuestion->pivot->required == '1'),
                        'form-control',
                        $customQuestion->type)
                    !!}
    
                @else
                    {!! $customQuestion->getHtmlInput(
                        $customQuestion->name,
                        [],
                        ($customQuestion->pivot->required == '1'),
                        'form-control',
                        $customQuestion->type)
                    !!}
                @endif
                <input type="hidden"
                       name="participant_question_required[]"
                       value="{{ $customQuestion->pivot->required }}">
                <input type="hidden"
                       value="{{ $customQuestion->id }}"
                       name="participant_question_id[]"/>
            </div>
        @endforeach
    @endif
    ....
    </form>

The storeRegistrationInfo() that validates if a custom question that is required was not answered:

    public function storeRegistration(Request $request, $id, $slug = null, Validator $validator)
        {
            $user = Auth::user();
    
            $rules = [];
            $messages = [];
    
            if (isset($request->participant_question_required)) {
    
                dd($request->participant_question_required);
    
                $messages = [
                    'participant_question.*.required' => 'Fill all mandatory fields',
                ];
    
                foreach ($request->participant_question_required as $key => $value) {
                    $rule = 'string|max:255';

                     //var_dump($value);  shows string(1) "1" string(1) "1" string(1) "0" string(1) "0" string(1) "0" string(1) "1"

                    // if this was required, ie 1, prepend "required|" to the rule
                    if ($value) {

                      //var_dump($value); // shows string(1) "1" string(1) "1" string(1) "1"

                        $rule = 'required|' . $rule;
                    }
                    $rules["participant_question.{$key}"] = $rule;
                }
            }
    
            $validator = Validator::make($request->all(), $rules, $messages);
    
            if ($validator->passes()) {
                dd('test');  // this dont appears
                // create an entry in the registrations table
                // create an entry in the participants table for each registered participant
                // create a entry in the answer table for each answer to a custom question
            }
        }

The "dd($request->participant_question_required);" in "if (isset($request->participant_question_required)) {...}" shows:

    array:6 [▼
      0 => "1"
      1 => "1"
      2 => "0"
      3 => "0"
      4 => "0"
      5 => "1"
    ]

In the database pivot table registration_type_questions is:

    id  registration_type_id   question_id    required
    1             1                1                1    (1 means required)
    2             1                2                1    
    3             1                3                0    (0 not required)
    4             1                4                0   
    5             1                5                0   
    6             1                6                1   

0 likes
1 reply
adamjhn's avatar

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

MessageBag {#298 ▼
  #messages: array:3 [▼
    "participant_question.0" => array:1 [▼
      0 => "Fill all mandatory fields"
    ]
    "participant_question.1" => array:1 [▼
      0 => "Fill all mandatory fields"
    ]
    "participant_question.5" => array:1 [▼
      0 => "Fill all mandatory fields"
    ]
  ]
  #format: ":message"
}

HTML of the form in question:

<form method="post" action="http://proj.test/conference/1/conf-title/registration/storeRegistration">
  <input type="hidden" name="_token" value="">

  <div class="form-group">
    <label for="participant_question">Text type question</label>

    <input type="text" name="participant_question" class="form-control" required="">
    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="1" name="participant_question_id[]">
  </div>
  
<div class="form-group">
    <label for="participant_question">Checkbox type question</label>

    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check1" class="form-check-input" required="">    <label class="form-check-label" for="exampleCheck1">check1</label></div> 
    <div class="form-check">
      <input type="checkbox" name="participant_question[]" value="check2" class="form-check-input" required="">    <label class="form-check-label" for="exampleCheck1">check2</label></div>

    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="2" name="participant_question_id[]">
  </div>
  
<div class="form-group">
    <label for="participant_question">Radio button type question</label>

    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad1" class="form-check-input">    <label class="form-check-label" for="exampleCheck1">rad1</label></div> 
    <div class="form-check">
      <input type="radio" name="participant_question[]" value="rad2" class="form-check-input">    <label class="form-check-label" for="exampleCheck1">rad2</label></div>

    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="3" name="participant_question_id[]">
  </div>
  
<div class="form-group">
    <label for="participant_question">Select menu type question</label>
    <select name="participant_question" class="form-control" required=""><option value="select1">select1</option><option value="select2">select2</option></select>

    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="4" name="participant_question_id[]">
  </div>
  
<div class="form-group">
    <label for="participant_question">textarea type question</label>
    <textarea name="participant_question" class="form-control" rows="3"></textarea>
    <input type="hidden" name="participant_question_required[]" value="0">
    <input type="hidden" value="5" name="participant_question_id[]">
  </div>
  
<div class="form-group">
    <label for="participant_question">file type question</label>
    <input type="file" name="participant_question" class="form-control" required="">

    <input type="hidden" name="participant_question_required[]" value="1">
    <input type="hidden" value="6" name="participant_question_id[]">
  </div>

  <input type="submit" href="#step2" class="btn btn-primary" value="Store Registration">
</form>

Please or to participate in this conversation.