编辑:我的$ id包含一个数组
@php($ids = array('Fruit','Vegetables')
我只想在页面返回时根据我的验证显示错误复选框和textarea的旧输入。 这是表单元素。我尝试使用旧的(方法),但它不适合我。
<form method = "post" action = "tomyController">
<input type = "text" name = "vendor" >
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0">
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1">
<textarea name = "remarks[{{id}}]"></textarea>
@endforeach
</form>
我尝试了How to show old data of checkbox in Laravel?,但没有对我的工作。 这是我实现它的方式。
<form method = "post" action = "tomyController">
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0"
@if(is_array(old('check[$id][0]')) && in_array(0, old('check[$id][0]'))) checked @endif)>
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1"
@if(is_array(old('check[$id][1]')) && in_array(1, old('check[$id][1]'))) checked @endif)>
<textarea name = "remarks[{{id}}]">{{ old('remarks[$id]') }}</textarea>
@endforeach
</form>
我的控制器代码在这里。
public function store(Request $request) {
$post = $request->all();
$validator = Validator::make($request->all(), [
"vendor" => "required",
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput($post);
}
else
#my other codes
}
答案 0 :(得分:0)
将您的form
更改为以下内容 -
<form method = "post" action = "tomyController">
@foreach($ids as $id)
<input type = "checkbox" name = "check[{{$id}}][0]" value = "0"
@if(is_array(old('check['.$id.']')) && (0==old('check['.$id.'][0]'))) checked @endif>
<input type = "checkbox" name = "check[{{$id}}][1]" value = "1"
@if(is_array(old('check['.$id.']')) && (1== old('check['.$id.'][1]'))) checked @endif>
<textarea name = "remarks[{{$id}}]">{{ old('remarks['.$id.']') }}</textarea>
@endforeach
</form>