0

Edit: my $id contains an array

@php($ids = array('Fruit','Vegetables')

I just want to display the old input of my checkbox and textarea when the page returns with errors based on my validations. Here is the form elements. I tried using the old(method) but its not working for me.

<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>

I tried the How to show old data of checkbox in Laravel? but didnt work on mine. Here is how i implemented it.

<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>

My controller code is here.

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)

jerome
  • 695
  • 6
  • 20

1 Answers1

2

Change your form to the following-

   <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>
Sohel0415
  • 9,523
  • 21
  • 30
  • could you please show what happens after adding this to your code like any error?? if no error, then show the html page from browser source code – Sohel0415 Jan 23 '18 at 06:16
  • not this, your source code generated html and what your output should show?? – Sohel0415 Jan 23 '18 at 06:31
  • What do you mean source code generated html ? The output would be like. If the vendor field is leave as blank it upon submission the program would show an error message and will remember the users previous inputs that will be entered automatically to the fields. – jerome Jan 23 '18 at 06:36
  • right click on page and click view page source and your old input is working i think as your check boxes are checked on view you give – Sohel0415 Jan 23 '18 at 06:39
  • https://ibb.co/bLacdb i just tick the checkboxes when i screencaptured it – jerome Jan 23 '18 at 06:50