Reputation: 95
I'm still new in Laravel. I already tried validate my array field but it seems unsuccessful and gives an error (something about Countable at line if (count($request->defect_id) > 0)
if i remove required
in my blade view). Here is my code:
ComplaintController.php
public function store(Request $request)
{
if (count($request->defect_id) > 0) {
foreach ($request->defect_id as $item => $v) {
if (isset($request->image[$item])) {
$images = $request->file('image');
$image_resize = Image::make($images[$item]->getRealPath());
$image_resize->resize(900, 630);
$filename = $images[$item]->getClientOriginalName();
$image_resize = Image::make($images[$item]->getRealPath());
$image_resize->resize(300, 210);
$image_resize->save('complaint/' . $filename);
}
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
);
Complaint::insert($data);
}
return redirect()->back()->with('success', 'Your report is submitted!');
}
}
What I have tried for the validation:
$this->validate($request,[
'defect_id'=>'exists:complaints,defect_id',
'image.*'=>'mimes:jpeg,png,jpg,gif,svg|max:10000',
'description'=>'required|max:255',
]);
$validator = Validator::make($request->all(),[
'defect_id' => 'required|array',
'image' => 'required|array|image|mimes:jpeg,png,jpg,gif,svg',
'description' => 'required|array|max:255',
]);
$rules = [
'defect_id' => 'required',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
'description' => 'required|max:255',
];
$messages = [
'defect_id.required' => 'Types of defect is required',
'image.required' => 'Image is required',
'description.required' => 'Description is required',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return Response::json(array('errors' => $validator->getMessageBag()->toArray()));
}
else {
$data = array(
'defect_id' => $request->defect_id[$item],
'image' => $filename,
'description' => $request->description[$item],
}
form.blade.php
<div class="panel-heading">
<h3 class="panel-title"><strong>Make New Report</strong></h3>
</div>
<div class="panel-body">
<div>
<div class="panel">
<form action="{{ url('/complaint-store') }}" method="post" enctype="multipart/form-data">
@csrf
<table class="table">
<thead>
<tr>
<th>Type of Defect</th>
<th>Image</th>
<th>Description</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control" name="defect_id[]" required>
<option disabled selected>Choose defect</option>
@foreach(App\Defect::all() as $defect)
<option value="{{$defect->id}}">{{$defect->name}}</option>
@endforeach
</select>
</td>
<td>
<input type="file" class="form-control-file" name="image[]" required>
</td>
<td>
<textarea class="form-control" rows="2" name="description[]" required></textarea>
</td>
<td>
<button type="button" class="btn btn-info btn-sm" id="add-btn"><i class="glyphicon glyphicon-plus"></i></button>
</td>
</tr>
</tbody>
</table>
<center><button type="submit" class="btn btn-primary" >Submit</button></center>
<br>
</form>
</div>
</div>
</div>
@section('footer')
<script>
$(document).ready(function () {
$('#add-btn').on('click',function () {
var html = '';
html += '<tr>';
html += '<td><select class="form-control" name="defect_id[]" required><option value="" selected>Choose Defect</option>@foreach(App\Defect::all() as $defect)<option value="{{$defect->id}}">{{$defect->name}}</option>@endforeach</td>';
html += '<td><input type="file" class="form-control-file" name="image[]" required></td>';
html += '<td><textarea class="form-control" rows="2" name="description[]" required></textarea></td>';
html += '<td><button type="button" class="btn btn-danger btn-sm" id="remove-btn"><i class="glyphicon glyphicon-minus"></i></button></td>';
html += '</tr>';
$('tbody').append(html);
})
});
$(document).on('click','#remove-btn',function () {
$(this).closest('tr').remove();
});
</script>
@stop
I hope someone can help me in this code. Thank you in advance.
Upvotes: 0
Views: 191
Reputation: 136
This is because $request->defect_id
is null
,
count
is not working on null
value (since php 7+)
you may change as below: check $request->defect_id
first, then count.
if ($request->defect_id != null && count($request->defect_id) > 0) {
....
}
Upvotes: 1