我有多个输入,我可以通过单击按钮(+)在任意位置克隆div来添加更多内容。
我需要它,以便在保存表单时数据为空,并使用所有div和输入将其重定向回。 例如:添加五个输入,然后单击“提交并重定向”时,我需要显示此输入(五个输入)。
这是我的示例代码:
<form class="m-form m-form--fit m-form--label-align-right" id="m_form_1" method="post">
<div class="m-portlet__body">
@if(is_array(old('name')) && count(old('name')) > 0)
<div id="countriesContainer">
@foreach(old('name') as $key => $item)
<div class="form-group m-form__group row countries">
<label class="col-form-label col-lg-2">{{ trans('general.language') }}</label>
<div class="col-2">
<select class="form-control m-bootstrap-select m_selectpicker" data-live-search="true" name="language[]">
<option value="">{{ trans('general.select_item') }}</option>
@foreach($language as $row)
<option value="{{ $row->locale }}" {{ old('language')[$item] == $row->locale ? 'selected' : '' }}>{{ $row->name }}</option>
@endforeach
</select>
</div>
<div class="col-lg-6">
<input type='text' class="form-control m-input lang" name="name" value="{{ old('name')[$item] }}" placeholder="{{ trans('countries.name') }}"/>
</div>
<div class="col-2">
<a href="javascript:;" class="btn btn-brand m-btn m-btn--custom add">
<i class="fa fa-plus"></i>
</a>
<a href="javascript:;" class="btn btn-danger m-btn m-btn--custom remove">
<i class="fa fa-minus"></i>
</a>
</div>
</div>
@endforeach
</div>
@else
<div id="countriesContainer">
<div class="form-group m-form__group row countries">
<label class="col-form-label col-lg-2">{{ trans('general.language') }}</label>
<div class="col-2">
<select class="form-control m-bootstrap-select m_selectpicker" data-live-search="true" name="language">
<option value="">{{ trans('general.select_item') }}</option>
@foreach($language as $row)
<option value="{{ $row->locale }}" {{ old('language') == $row->locale ? 'selected' : '' }}>{{ $row->name }}</option>
@endforeach
</select>
</div>
<div class="col-lg-6">
<input type='text' class="form-control m-input lang" name="name" placeholder="{{ trans('countries.name') }}"/>
</div>
<div class="col-2">
<a href="javascript:;" class="btn btn-brand m-btn m-btn--custom add">
<i class="fa fa-plus"></i>
</a>
<a href="javascript:;" class="btn btn-danger m-btn m-btn--custom remove">
<i class="fa fa-minus"></i>
</a>
</div>
</div>
</div>
@endif
</div>
</form>
这是我的控制器代码:
$language = $request->get('language');
$name = $request->get('name');
$status = (int)$request->get('status');
$validator = Validator::make($request->all(), [
"name.*" => "required",
]);
答案 0 :(得分:0)
<input name="name" ...
应该是<input name="name[]" ...
,如果存在多个具有相同名称的输入字段,则它应该是一个数组。
要获得旧值,您需要loop index。不是$item
所以应该是
<input name="name[]" value="{{ old('name')[$loop->index] }}" ...