Php 在一个视图中处理多个动态表单(非输入)-Laravel
我有一个带有动态输入表单的页面,用户可以在其中添加和“删除”行。(我所说的“删除”,是指它从前端删除,但实际上并不从数据库中删除。)该功能可以更新现有行和添加新行。我遇到的问题是删除行 我希望有一个模式弹出窗口,确认他们想在删除行之前删除它。我的第一个想法是在现有行中循环,并使用表单为每行创建相应的模式,但我一直在设置控制器,以识别它要删除的表单/行-因此,要重述一下,将有一个具有动态字段的表单(这就是现在可以工作的表单),将有多个动态表单用于删除行 这是我的HTML:Php 在一个视图中处理多个动态表单(非输入)-Laravel,php,laravel,forms,Php,Laravel,Forms,我有一个带有动态输入表单的页面,用户可以在其中添加和“删除”行。(我所说的“删除”,是指它从前端删除,但实际上并不从数据库中删除。)该功能可以更新现有行和添加新行。我遇到的问题是删除行 我希望有一个模式弹出窗口,确认他们想在删除行之前删除它。我的第一个想法是在现有行中循环,并使用表单为每行创建相应的模式,但我一直在设置控制器,以识别它要删除的表单/行-因此,要重述一下,将有一个具有动态字段的表单(这就是现在可以工作的表单),将有多个动态表单用于删除行 这是我的HTML: <form act
<form action="{{route('preliminary-children.updateChildren')}}" method="POST">
@csrf
<!-- Content Row -->
<div class="row" id="childInfo">
<!-- About Process -->
<div class="col-xl-12 col-lg-12">
<div class="card shadow mb-4">
<!-- Card Header - Dropdown -->
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<h6 class="m-0 font-weight-bold text-secondary">Family Information - Children</h6>
</div>
<div id="dynamic_field">
@foreach($children as $i => $child)
<div class="card-body" id="row{{$i+1}}">
@if ($i != 0)
<hr/>
<div class="text-right px-2 my-2 child-{{$i+1}}">
<a href="#" class="text-decoration-none" data-toggle="modal" data-target="#remove{{$i+1}}">
<button type="button" class="px-0 btn btn-circle btn-danger">
<i class="fal fa-times"></i>
</button>
</a>
</div>
<div class="modal fade" id="remove{{$i+1}}" tabindex="-1" role="dialog" aria-labelledby="label{{$i+1}}" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="label{{$i+1}}">Delete Row</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to remove this row?</div>
<div class="modal-footer">
<button class="btn bg-gray-300" type="button" data-dismiss="modal">Cancel</button>
<form id="remove-form{{$i+1}}" name="formrow[]" action="{{route('preliminary-children.updateChildren')}}" method="POST" class="d-inline-block">
<input type="text" name="removeID[]" id="removeID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<button type="submit" id="{{$i+1}}" name="removerow[]" class="btn btn-danger text-decoration-none btn_remove">Remove</button>
</form>
</div>
</div>
</div>
</div>
@endif
<input type="text" name="userID[]" id="userID{{$i+1}}" value="{{ $user->id }}" hidden="hidden">
<input type="text" name="ID[]" id="childID{{$i+1}}" value="{{$child->id}}" hidden="hidden">
<input type="text" name="rel[]" id="rel{{$i+1}}" value="child" hidden="hidden">
<div class="form-row">
<div class="form-group col-md-6">
<label for="first">First Name</label>
<input name="first[]" type="text" class="form-control" id="first{{$i+1}}" value="{{$child->first_name}}" placeholder="First Name">
</div>
<div class="form-group col-md-6">
<label for="last">Last Name</label>
<input name="last[]" type="text" class="form-control" id="last{{$i+1}}" value="{{$child->last_name}}" placeholder="Last Name">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputSSN">SSN</label>
<input data-inputmask="'mask': '999-99-9999'" value="{{$child->ssn}}" type="text" class="form-control" name="inputSSN[]" id="SSN{{$i+1}}" placeholder="SSN">
</div>
<div class="form-group col-md-6">
<label for="dob">Date of Birth</label>
<input data-inputmask="'mask': '99/99/9999'" type="datepicker" value="{{$child->dob}}" class="form-control" name="dob[]" id="do{{$i+1}}B" placeholder="Date of Birth">
</div>
</div>
<div class="form-row">
<div class="form-group col-md">
<label for="inputGender">Gender</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="inputGender[]" id="Gender{{$i+1}}" placeholder="Gender">
@foreach($genders as $gender)
<option value="{{$gender}}" @if($child){{ ($child->gender == $gender) ? 'selected' : ''}} @endif>{{$gender}}</option>
@endforeach
</select>
</div>
<div class="form-group col-md">
<label for="primaryHeight">Height</label>
<br/>
<select class="form-control d-inline-block w-25 mr-1" name="primaryFeet[]" id="primaryFeet{{$i+1}}">
@foreach($feet as $foot)
<option value="{{$foot}}" @if($child){{($child->height_feet == $foot) ? 'selected' : ''}} @endif>{{$foot}}</option>
@endforeach
</select>
<span class="d-inline-block w-10 mr-3">ft</span>
<select class="form-control d-inline-block w-25 mr-1" name="primaryInches[]" id="primaryInches{{$i+1}}">
@foreach($inches as $inch)
<option value="{{$inch}}" @if($child){{($child->height_inch == $inch) ? 'selected' : ''}} @endif>{{$inch}}</option>
@endforeach
</select>
<span class="d-inline-block w-10">in</span>
</div>
<div class="form-group col-md">
<label for="primaryWeight">Weight (lbs)</label>
<br/>
<input type="number" min="0" step=".1" class="form-control d-inline-block w-75 mr-1" id="primaryWeight{{$i+1}}" value="{{$child->weight_lbs}}" name="primaryWeight[]" placeholder="Weight (lbs)">
<span class="d-inline-block w-10">lbs</span>
</div>
</div>
</div>
@endforeach
</div>
<hr/>
<div class="text-center my-4">
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
</div>
</div>
</div>
</div>
<div class="form-row mb-4 text-center">
<div class="form-group col-md-12 mt-4">
<a href="{{ route('preliminary-spouse') }}" class="btn bg-gray-300">Go Back</a> <button type="submit" name="savecontinue" class="btn btn-info">Save and Continue</button>
</div>
</div>
</form>
我唯一尝试过的就是设置一个if/elseif($request->has('savecontinue'){…}
并将我现有的控制器包装在其中。else{}
将处理所有的modals,但它不断地回退错误,好像我所有的语法都不正确一样
想法?如果有更好的方法,我也不反对以不同的方式处理这个问题
PS,删除和添加按钮有JS,但我认为这个问题没有必要。如果你需要我用它更新这个,我可以。;删除子项与更新子项是不同的操作,因此你应该在控制器中使用不同的方法。我不确定你是如何命名的,但这应该会给你一个想法 定义新路线:
Route::delete('/whatever/{child}',[ChildController::class,'deleteChild'])
->名称('premium-children.deleteChild');
注意,我们将使用访问此路由,ID作为传递
更新视图以指向此新路线:
删除行
×
确实要删除此行吗?
取消
去除
web浏览器无法发送删除请求,因此我们需要隐藏输入
然后我们做一个方法:
公共函数deleteChild(UserFamily$child)
{
$child->delete();
返回响应()->json(“成功”);
}
通过类型提示参数,Laravel为您提供。当然,你也会想在那里做一些错误检查。你太棒了。非常感谢。感谢您提供了链接,说明您为什么要做每一步。这确实很有帮助此外,它不允许我编辑您的答案,只需指出一个输入错误-对于删除输入,您有两个
type=“”
属性。我知道了,但我想提一下,以防万一有人用了它,却看不到。:)其中一个应该是名称
属性。现在修好了,很高兴我能帮忙。
public function updateChildren(Request $request)
{
$id = Auth::user()->id;
$user = Auth::user();
$family = UserFamily::where(['user_id'=> $id, 'rel' => 'child'])->get();
$count = count($family);
$children = ($count > 0 ? $family : '');
$input = $request->all();
$rules = [
'first[*]' => 'required',
'userID[*]' => 'required',
'rel[*]' => 'required',
'first[*]' => 'required',
'last[*]' => 'required',
'inputSSN[*]' => 'required',
'dob[*]' => 'required',
'inputGender[*]' => 'required',
'primaryFeet[*]' => 'required',
'primaryInches[*]' => 'required',
'primaryWeight[*]' => 'required',
];
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
foreach($input['userID'] as $index => $value) {
$feet = $input['primaryFeet'][$index];
$inch = $input['primaryInches'][$index];
$weight = $input['primaryWeight'][$index];
$inches = ($feet * 12) + $inch;
$bmi = number_format(((703 * $weight) / ($inches * $inches)), 2, '.', '');
if(isset($input['ID'][$index])){
$famid = $input['ID'][$index];
}else {
$famid = '';
}
if($famid > 0 || $famid != '') {
$user = UserFamily::firstOrNew(['user_id'=> $id, 'rel' => 'child', 'id' => $famid]);
}else{
$user = UserFamily::create(['user_id'=> $id, 'rel' => 'child']);
}
$user->user_id = $id;
$user->bmi = $bmi;
$user->first_name = $input['first'][$index];
$user->dob = $input['dob'][$index];
$user->last_name = $input['last'][$index];
$user->rel = $input['rel'][$index];
$user->ssn = $input['inputSSN'][$index];
$user->gender = $input['inputGender'][$index];
$user->height_feet = $input['primaryFeet'][$index];
$user->height_inch = $input['primaryInches'][$index];
$user->weight_lbs = $input['primaryWeight'][$index];
$user->save();
}
return redirect()->route('preliminary-review');
}
return redirect()->route('preliminary-children')->withErrors($validator);
}