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

时间:2018-08-11 14:13:58

标签: php arrays laravel

我所处的环境是:会议可以具有一种或多种注册类型,而用户可以在会议中以一种或多种会议的注册类型进行注册。

例如,id为1的会议具有两种关联的注册类型,id为1的注册类型为“ general”,id为2的“ plus”为注册类型。“ 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"
    ]
  ]
]

工作正常,所有信息都可以使用下面的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"
  ]
]

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

The field name is mandatory.
The field surname is mandatory.
Please answer the custom questions.

你知道为什么吗?用户填写所有这些字段,但似乎显示验证错误。

下面是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');
    }
}

注册表格:

<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 个答案:

答案 0 :(得分:1)

Change name="participant[{{$counter}}][surname]" to name="participant[surname]" 
and
Change name="participant[{{$counter}}][name]" to name="participant[name]"

,然后进行验证。

了解您要发送的内容

如果您重新检查$request,则会看到$participent is multidimentional array,只有第一个数组具有名称索引,而第二个数组不包含name,因此会引发错误。

验证participent.*.name只需要这样的一维名称数组:

$participent[0] = 'name1'
$participent[1] = 'name2'
$participent[2] = 'name3'
$participent[3] = 'name4' 

这意味着$participent数组的每个索引必须具有name索引,但是您将其发送错误,如下所示。

$participent[0] = [name=> somename, ...]
$participent[1] = [question=>'abc', answer => 'abc']

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

答案 1 :(得分:1)

使用以下表单字段更新您的注册表单:

  • participant[name]更改为participant_name[user_index]。 [例如。 participan_name[1]],其中user_index是数量索引。

  • participant[surname]更改为participant_surname[user_index]。 [例如。 participan_surname[1]],其中user_index是数量索引。

  • participant[1][answer]更改为answer[user_index][question_id]。 [例如。 participan_surname[1][5]],其中user_index是数量索引,question_id是问题的primary_key。

  • participant_question_required[]更改为participant_question_required[question_id]。 [例如。 participant_question_required[5]],其中question_id是问题的primary_key。如果需要,请将其值设置为1,否则将其设置为0

  • 您可以忽略participant[1][question_id]。[在这种方法中,此字段不是必需的]

使用以下方法更新控制器验证器:

$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);

希望有帮助。