0

编辑:我的 $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>

我尝试了如何在 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
}

dd($请求)

4

1 回答 1

2

将您更改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>
于 2018-01-23T05:52:18.267 回答