我尝试使用return - > withInput(),但它不起作用我的HTML和控制器我正在使用laravel 5.4并且在我的表单中我也使用追加循环数据。
<form action="{{route('subject.store')}}" method="POST">
{{ csrf_field() }}
<div class="form-group row">
<label class="col-sm-3 col-form-label">SUb</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="subject[]" placeholder="">
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">credit</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="credit[]" placeholder="">
</div>
</div>
<hr>
<div id="add-me"></div>
<div class="form-group row mb-0">
<div class="offset-sm-3 col-sm-9">
<button id="add-form" type="button" class="btn btn-default">addmore</button>
<button type="submit" class="btn btn-primary">save</button>
</div>
</div>
</form>
</div>
并在我的控制器中
$this->validate($request, [
'subject.*' => 'required',
'credit.*' => 'required',
],
[
'subject.*.required' => 'The category name field is required.',
'credit.*.required' => 'The sss name field is required.',
]
);
return redirect()->back()->withInput()->withErrors($validator);
我的javascript
$(document).ready(function() {
$('#add-form').click(function() {
i++;
id_i++;
$('#add-me').append(
'<div class="form-group row">'
+'<label class="col-sm-3 col-form-label">sub</label>'
+'<div class="col-sm-9">'
+'<input type="text" class="form-control" name="subject[]" placeholder="">'
+'</div>'
+'</div>'
+'<div class="form-group row">'
+'<label class="col-sm-3 col-form-label">credit</label>'
+'<div class="col-sm-9">'
+'<input type="text" class="form-control" name="credit[]" placeholder="">'
+'</div>'
+'</div>'
+'<hr>'
);
});
});
当我提交数据时如果有一些验证我怎么能用旧输入返回
答案 0 :(得分:0)
使用old
功能获取
实施例
@if(count(old('subject', [])))
@foreach(old('subject') as $subject)
<div class="form-group row">
<label class="col-sm-3 col-form-label">SUb</label>
<div class="col-sm-9">
<input type="text" value="{{ $subject }}" class="form-control" name="subject[]" placeholder="">
</div>
</div>
@endforeach
@else
<div class="form-group row">
<label class="col-sm-3 col-form-label">SUb</label>
<div class="col-sm-9">
<input type="text" class="form-control" name="subject[]" placeholder="">
</div>
</div>
@endif