如何为每个参与者正确获取自定义问题的姓名和答案,以便在DB中引入所有信息?

6 php arrays laravel

我有一个上下文,其中会议可以有一个或多个注册类型,并且用户可以在一个或多个会议注册类型的会议中进行注册.

例如,id为1的会议有两种注册类型,注册类型为"general",id为1,"plus"为id 2.注册类型"general"有6个自定义问题,注册类型为"加上"没有任何与之相关的自定义问题.

当用户在会议中进行注册并为注册类型"加"选择数量"2"并单击"下一步"时,用户进入注册页面.在此页面中,有注册表单,用户只需输入他的姓名和姓氏,然后单击"注册".在$request->all像下面所示,参与者的数组存储了姓名,和登记每个参与者的类型:

array:4 [?
  "participant" => array:2 [?
    1 => array:3 [?
      "name" => "John"
      "surname" => "W"
      "rtypes" => "2"
    ]
    2 => array:3 [?
      "name" => "Jake"
      "surname" => "K"
      "rtypes" => "2"
    ]
  ]
]
Run Code Online (Sandbox Code Playgroud)

它工作正常使用storeRegistrationInfo()下面的方法正确存储所有信息.

怀疑:

但是,如果用户正在进行注册并为注册类型"一般"选择数量"2"并单击"下一步",则注册类型"一般"有6个与之关联的自定义问题.因此,在注册表单中,用户需要输入他的姓名和姓氏,但用户还需要回答6个必需的自定义问题.我怀疑的是如何为每个参与者存储自定义问题的答案,您知道如何正确实现这一点吗?因为实际上,在用户输入名称,姓氏和6个自定义问题的答案并单击"下一步"后,$request->all()不显示正确的信息,它显示如下:

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"
  ]
]
Run Code Online (Sandbox Code Playgroud)

因此,不是storeRegistration()存储所有必要的信息,它总是出现3个验证错误:

The field name is mandatory.
The field surname is mandatory.
Please answer the custom questions.
Run Code Online (Sandbox Code Playgroud)

你知道为什么吗?用户会修改所有这些字段,但似乎验证错误.

下面是RegistrationController storeRegistration()存储注册信息的方法:

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');
    }
}
Run Code Online (Sandbox Code Playgroud)

报名表格:

<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>
Run Code Online (Sandbox Code Playgroud)

Afr*_*mad 1

Change name="participant[{{$counter}}][surname]" to name="participant[surname]" 
and
Change name="participant[{{$counter}}][name]" to name="participant[name]"
Run Code Online (Sandbox Code Playgroud)

并且验证将会起作用。

了解您要发送的内容

如果您重新检查,$request那么您将看到$participent is multidimentional array只有第一个数组具有名称索引,但第二个数组不包含name,因此它会抛出错误。

验证participent.*.name只需要一维名称数组,如下所示:

$participent[0] = 'name1'
$participent[1] = 'name2'
$participent[2] = 'name3'
$participent[3] = 'name4' 
Run Code Online (Sandbox Code Playgroud)

这意味着$participent数组的每个索引都必须有name索引,但您发送的错误如下。

$participent[0] = [name=> somename, ...]
$participent[1] = [question=>'abc', answer => 'abc']
Run Code Online (Sandbox Code Playgroud)

因此,第二个索引$participent array没有name索引,而您的验证需要它。