我的表格分为两部分。 表格的第一部分显示为" GET"用户单击按钮时请求。当用户填写此表单并单击下一步时,页面将被重定向到表单的下一部分,其中包含" POST"首先保存表单然后显示下一部分的请求。 问题是当我尝试验证表单的第二部分时,laravel给出并且错误说不允许使用方法。
我的第一个控制员:
public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'unit_code' => 'max:25',
'unit_title' => 'max:255',
'project_title' => 'max:255',
'project_ref_number' => 'max:255',
'storage_location' => 'required|max:255',
'keeper_name' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
$notification = Notification::create([
'unit_code' =>$request->unit_code,
'unit_title' =>$request->unit_title,
'project_title' =>$request->project_title,
'project_ref_number'=>$request->project_ref_number,
'storage_location' =>$request->storage_location,
'keeper_name' =>$request->keeper_name,
'user_id' =>Auth::user()->id
]);
return view('Notification.notification_for_lmo')->with('notification', $notification);
//return redirect()->route('show.material_List')->with('notification');
}
路线:
/*route to personal information form for notification*/
Route::get('/personal_information_notification_form', 'HomeController@getNotificationForm');
/*submit personal information and go to next part of the form*/
Route::post('personal_information_notification_form/submit/', 'NotificationController@create')->name('submit.personal_info_for_notification');
/*route to material list form for notification application*/
Route::get('personal_information_notification_form/material_list/', 'NotificationController@showMaterialListForm')->name('show.material_List');
/*submit the material list*/
Route::post('/personal_information_notification_form/add_lmo/', 'NotificationTypeAController@create')->name('submit.add_lmo');
第二控制器:
public function create(Request $request)
{
/*this loop is because im adding rows dynamically to the table*/
$count = count($request->input('item_name'));
for ($i=0; $i<$count; $i++){
$validator = Validator::make($request->all(), [
'material_type' => 'required|max:25',
'item_name' => 'required|max:255',
'risk_level' => 'required|max:255',
'quantity' => 'required|max:255',
'volume' => 'required|max:255',
'notification_id' => 'required|max:255',
]);
}
if ($validator->fails()) {
return view('Notification.notification_for_lmo')->withErrors($validator)->withInput();
}
for ($i=0; $i<$count; $i++){
$data = NotificationTypeA::create([
'material_type' =>$request->material_type[$i],
'item_name' =>$request->item_name[$i],
'risk_level' =>$request->risk_level[$i],
'quantity' =>$request->quantity[$i],
'volume' =>$request->volume[$i],
'notification_id'=>$request->notification_id
]);
}
$admin = Admin::find(1);
$user = Auth::user();
$notification = Notification::find($request->notification_id);
$admin->notify(new NewNotificationApplicationSubmitted($user->name, $notification->id));
return redirect()->route('show.go_to_notification')->with('message', 'We have notified '.$user->name.' that he/she is added to SSBC')->with('status', 'info');
}
在第二个控制器中,有一个for循环,因为用户以表格形式填充数据。 (刀片文件很大,所以我不在这里发布。)
这有什么问题,为什么会发生这种情况?
第一刀片:
<div class="well">
<form class="form-horizontal" role="form" method="POST" action="{{route('submit.personal_info_for_notification')}}">
{{ csrf_field() }}
<fieldset>
<legend>
SECTION 1 - Personal Details
</legend>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<div class="col-xs-12">
{!! Form::label('unit_code', 'Unit Code (if teaching):', ['class' => 'control-label']) !!} <br>
{!! Form::text('unit_code', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col-xs-12">
{!! Form::label('unit_title', 'Unit Title (if teaching):', ['class' => 'control-label']) !!} <br>
{!! Form::text('unit_title', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col-xs-12">
{!! Form::label('project_title', 'Project Title (if FYP/research):', ['class' => 'control-label']) !!} <br>
{!! Form::text('project_title', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col-xs-12">
{!! Form::label('project_ref_number', 'Ref. No (if FYP/research):', ['class' => 'control-label']) !!} <br>
{!! Form::text('project_ref_number', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col-xs-12">
{!! Form::label('storage_location', 'Storage Location:', ['class' => 'control-label']) !!} <br>
{!! Form::text('storage_location', $value = null, ['class' => 'form-control' ]) !!}
</div>
<div class="col-xs-12">
{!! Form::label('keeper_name', 'Name of the Keeper:', ['class' => 'control-label']) !!} <br>
{!! Form::text('keeper_name', $value = null, ['class' => 'form-control' ]) !!}
</div>
</div>
</div>
</div>
</fieldset>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">Next</button>
</div>
</div>
</div>
</form>
</div>
第二刀片:
<form class="form-horizontal" role="form" method="POST" action="{{ route('submit.add_lmo') }}">
{{ csrf_field() }}
<fieldset>
<div class="row">
<div class="col-xs-12">
<div class="row">
<legend>
SECTION 2 – Details of the Biohazardous Materials
</legend>
<div class="col-xs-8">
<h4>List of Living Modified Organism (LMO)</h4>
</div>
<div class="col-xs-4">
<input type="checkbox"/>Applicable
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div id="LMOtablediv">
<input type="button" id="addmoreLMObutton" value="Add" onclick="insRow(event)" />
<table id="addLMOtable" border="1">
<thead>
<tr>
<td>No.</td>
<td>Notification ID</td>
<td>Material Type</td>
<td>Name</td>
<td>Risk Level</td>
<td>Quantity</td>
<td>Volume</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td><input type="text" name="notification_id" id="notification_id" value="{{$notification->id}}"></td>
<td>{!! Form::text('material_type[]', null, array('id'=>'material_type'))!!}</td>
<td>{!! Form::text('item_name[]', null, array('id'=>'item_name'))!!}</td>
<td>{!! Form::text('risk_level[]', null, array('id'=>'risk_level'))!!}</td>
<td>{!! Form::number('quantity[]', null, array('id'=>'quantity'))!!}</td>
<td>{!! Form::number('volume[]', null, array('id'=>'volume'))!!}</td>
<td><input type="button" id="delLMObutton" value="Delete" onclick="deleteRow(this)"/></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</fieldset>
<div class="row">
<div class="form-group">
<div class="col-md-6 col-md-offset-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
答案 0 :(得分:0)
material_type
列。要解决验证问题,请删除验证规则的循环,然后尝试以下操作:
$validator = Validator::make($request->all(), [
'material_type' => 'array',
'material_type.*' => 'required|max:25',
'item_name' => 'array',
'item_name.*' => 'required|max:255',
'risk_level' => 'array',
'risk_level.*' => 'required|max:255',
'quantity' => 'array',
'quantity.*' => 'required|max:255',
'volume' => 'array',
'volume.*' => 'required|max:255',
'notification_id' => 'array',
'notification_id.*' => 'required|max:255',
]);
if ($validator->fails()) {
return view('Notification.notification_for_lmo')->withErrors($validator)->withInput();
}
如果您确定所有输入都有值且没有空值,请确保Eloquent模型中的$fillable
数组中存在属性。