我看到两件事
1.-当$allParticipants==0时,表单不会发送任何内容,可能更像:
<form method="post" id="step1form" action="">
{{csrf_field()}}
@if (!is_null($allParticipants) && is_int($allParticipants))
@if($allParticipants == 1)
<p>Please fill in all fields. Your tickets will be sent to
p{{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
@else
<p>Its not necessary aditional info. Your tickets will be sent to {{ (\Auth::check()) ? Auth::user()->email : old('email')}}.</p>
@endif
@foreach($selectedTypes as $selectedType)
@foreach(range(1,$selectedType['quantity']) as $test)
<h6>Participant - 1 - {{$test}}</h6>
<div class="form-check">
<input class="form-check-input" type="radio" name="payment_method" value="referencias">
<label class="form-check-label d-flex align-items-center" for="exampleRadios1">
<span class="mr-auto">Fill the following fields with the authenticated user information.</span>
</label>
</div>
@if($allParticipants == 1)
<div class="form-group font-size-sm">
<label for="participant_name" class="text-gray">Name</label>
<input type="text" name="participant_name[]" required class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="participant_surname" class="text-gray">Surname</label>
<input type="text" required class="form-control" name="participant_surname[]" value="">
</div>
@foreach($selectedType['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
<input type="text"
@if($customQuestion->pivot->required == "1") required @endif
class="form-control" name="participant_question[]">
<input type="hidden" name="participant_question_required[]"
value="{{ $customQuestion->pivot->required }}">
<input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
</div>
@endforeach
@else
<input type="hidden" value="foo" name="participant_name[]"/>
<input type="hidden" value="bar" name="participant_surname[]"/>
@endif
<input type="hidden" name="ttypes[]" value="{{ $selectedType['id'] }}"/>
@endforeach
@if ($allParticipants == 0)
@foreach($selectedType['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
<input type="text"
@if($customQuestion->pivot->required == "1") required @endif
class="form-control" name="participant_question[]">
<input type="hidden" name="participant_question_required[]"
value="{{ $customQuestion->pivot->required }}">
<input type="hidden" value="{{ $customQuestion->id }}" name="participant_question_id[]"/>
</div>
@endforeach
@endif
@endforeach
@endif
<input type="submit" href="#step2"
id="goToStep2Free" class="btn btn-primary btn float-right next-step" value="Go to step 2"/>
</form>
2.-问题似乎取决于门票类型,是可选的,功能应该考虑到这一点。
public function StoreUserInfo(Request $request, $id, $slug = null, Validator $validator){
$allParticipants = Congress::where('id', $id)->first()->all_participants;
$user = Auth::user();
$rules = [];
$messages = [];
if(isset($request->participant_question_required)) {
$messages = [
'participant_question.*.required' => 'The participant is required'
];
foreach ($request->participant_question_required as $key => $value) {
$rule = 'string|max:255'; // I think string should come before max
//dd($value);
// 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;
}
}
if($allParticipants){
$rules = [
'participant_name.*' => 'required|max:255|string',
'participant_surname.*' => 'required|max:255|string',
];
}
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->passes()) {
$registration = Registration::create([
'congress_id' => $id,
'main_participant_id' => $user->id,
'status' => 'C',
]);
$participants = [];
for ($i = 0; $i < count($request->participant_name); $i++) {
$name = ($allParticipants) ? $request->participant_name[$i] : '';
$surname = ($allParticipants) ? $request->participant_surname[$i] : '';
$participants[] = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_id' => $registration->id,
'ticket_type_id' => $request->rtypes[$i]
]);
}
if (isset($request->participant_question))
for ($i = 0; $i < count($request->participant_question); $i++)
$answer = Answer::create([
'question_id' => $request->participant_question_id[$i],
'participant_id' => $participants[$i]->id,
'answer' => $request->participant_question[$i],
]);
}
return response()->json([
'success' => true,
'message' => 'success'
], 200);
}
希望它能起作用!
当做