Laravel return 复选框数组的旧输入
Laravel return old input of checkbox array
编辑:我的 $id 包含一个数组
@php($ids = array('Fruit','Vegetables')
我只想在页面 returns 根据我的验证出现错误时显示我的复选框和文本区域的旧输入。
这是表单元素。我尝试使用旧的(方法)但它对我不起作用。
<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>
我试过 但我的没用。
这是我实现它的方式。
<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($request)
将您的 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>
编辑:我的 $id 包含一个数组
@php($ids = array('Fruit','Vegetables')
我只想在页面 returns 根据我的验证出现错误时显示我的复选框和文本区域的旧输入。 这是表单元素。我尝试使用旧的(方法)但它对我不起作用。
<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>
我试过
<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($request)
将您的 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>