我在表格下方为用户输入一些信息以在国会中进行注册。
在下面的表格中有这一部分:
@foreach($selectedRtype['questions'] as $customQuestion)
<div class="form-group">
<label for="participant_question">{{$customQuestion->question}}</label>
<input type="text" @if($customQuestion->required == "1") required @endif class="form-control" name="participant_question[]" value="">
</div>
@endforeach
该代码针对每种故障单类型显示与该故障单类型相关联的自定义问题。并添加&#34; required&#34;如果在ticket_type_questions表中,必填字段为&#34; 1&#34;。
我的疑问是如何在RegistrationController storeRegistrationInfo()中验证,因为该字段可能是必需的,它取决于是&#34; 1&#34;或&#34; 0&#34;在列#34;必需&#34; ticket_type_questions表的。你知道如何处理这种情况吗?
public function storeRegistrationInfo(Request $request, $id, $slug = null, Validator $validator){
$validator = Validator::make($request->all(),[
'name' => 'required|max:255|string',
'surname' => 'required|max:255|string',
'email' => 'required|max:255|string',
'participant_name.*' => 'required|max:255|string',
'participant_surname.*' => 'required|max:255|string',
'participant_question.*' => '?????'
]);
...
}
表格关系问题的相关因素:
1 to many between congress and ticket types (a congress can have many ticket types)
1 to many between ticket types and ticket_type_questions (a ticket type can have many custom questions)
1 to many between questions and ticket_type_questions (a question can be associated with many ticket types)
ticket_type_questions表的示例:
id ticket_type_id question_id required
1 2 3 1 (means the ticket type with id 2 has the custom question 3 and is a required field)
//注册表格
<form method="post" action="">
{{csrf_field()}}
<div class="form-group font-size-sm">
<label for="name" class="text-gray">Name</label>
<input type="text" required class="form-control" id="name"
name="name" value="{{ (\Auth::check()) ? Auth::user()->name : old('name')}}">
</div>
<div class="form-group font-size-sm">
<label for="surname" class="text-gray">Surname</label>
<input type="text" id="surname" required class="form-control" name="surname" value="{{ (\Auth::check()) ? Auth::user()->surname : old('surname')}}">
</div>
<!-- other form fields -->
<!-- if the all_participants is 1 in the confernece table it should appear for each selected ticket a section for the user
that is doing the registration insert the name and surname of each paarticipant -->
@if (!empty($all_participants))
@if($all_participants == 1)
@foreach($selectedTypes as $k=>$selectedType)
@foreach(range(1, $selectedType['quantity']) as $test)
<h6>Participant - 1 - {{$k}}</h6> <!-- $k shows the ticket type name -->
<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" required class="form-control" name="participant_question[]" value="">
</div>
@endforeach
@endforeach
@endif
@endif
<input type="submit" href="#" value="Next"/>
</form>
答案 0 :(得分:0)
您可以使用以下工匠命令php artisan make:request RegisterRequest
创建一个Request类,您可以在该列中为该属性赋予可为空的属性,这意味着用户无需检查该特定输入。
Request Class看起来像这样,它基本上是一个验证类:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RegisterRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'nullable',
];
}
}
在此示例中,用户无需填写字段电子邮件。
此外,您只需在控制器功能参数中替换Request类,如下所示:
public function sent(RegisterRequest $request)
{
$data = $request->all();
....
}
当然,您必须像这样正确导入/使用它:
use App\Http\Requests\RegisterRequest;