Laravel验证跳过控制器中的循环



我有一个包含四张卡片的表单。我有一个控制器和一个函数来保存表单中的值。在控制器中,我有循环来查看哪些卡被选中,并根据所选的卡获得输入。如果没有验证,表单可以通过所有循环并将表单的输入保存在数据库中,但是当我将验证添加到函数中时,如果我选择了多个卡片,则错误验证将仅在第一个循环中发生,然后它跳过所有其他循环而不进行验证。我有办法解决这个验证错误吗?

控制器:

public function save_detail(Request $request)
{
// get user info in session
$user = $request->user();
$application = Application::create([
'app_type_id' => 1,
'approval_status_id' => 1
]);
$application->users()->save($user);
if ($request->has('emp_name')) {
$request->validate([
'emp_name'                  => 'required',
'emp_length'                => 'required',
'emp_monthly_gross_salary'  => 'required',
'monthly_allowance'         => 'required',
],[
'emp_name.required'                  => 'required',
'emp_length.required'                => 'required',
'emp_monthly_gross_salary.required'  => 'required',
'monthly_allowance.required'         => 'required',
]);
$user->employment_detail()->updateOrCreate(
['user_id' => $request->user()],
[
'emp_name' => $request->emp_name,
'emp_length' => $request->emp_length,
'emp_monthly_gross_salary' => sanitize_money($request->emp_monthly_gross_salary),
'monthly_allowance' => sanitize_money($request->monthly_allowance),
]
);
}
if ($request->has('role')) {

$request->validate([
'role'            => 'required',
'est_earnings'    => 'required',
],[
'role.required'                  => 'required',
'est_earnings.required'                => 'required',
]);
$user->part_time()->firstOrCreate(
[
'part_time_role' => $request->role,
'part_time_earning' => sanitize_money($request->est_earnings)
]
);
}
if ($request->has('inv_name')) {
$request->validate([
'inv_name'            => 'required',
'yearly_earning'    => 'required',
],[
'inv_name.required'                  => 'required',
'yearly_earning.required'                => 'required',
]);
$user->borrower_investment_income()->firstOrCreate(
[
'invest_source_specify' => $request->inv_name,
'invest_year_earning' => sanitize_money($request->yearly_earning)
]
);
}
$user->user_current_step = Constant::CURR_STEP_COMMITMENT;
$user->save();
alert('success','Your income are saved.');
return redirect()->back();
} 

foreach($value as $val)
{
$request->validate([
'emp_name'                  => 'required',
'emp_length'                => 'required',
'emp_monthly_gross_salary'  => 'required',
'monthly_allowance'         => 'required',
],[
'emp_name.required'                  => 'required',
'emp_length.required'                => 'required',
'emp_monthly_gross_salary.required'  => 'required',
'monthly_allowance.required'         => 'required',
]);
}

相关内容

  • 没有找到相关文章

最新更新