I’m trying to validate my checkbox from a loop, where at least one is checked and displays an error/alert or disable the submit button instead if there is no checkbox checked. I tried putting a required method inside my checkbox, but the checkbox is in a loop that’s why it requires all to be checked. That’s why I tried the script code below which I found on the internet but that doesn’t seem to work for me.
Below is my code in blade form
<form id="contact" action="{{url('/reservation')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="row col-12">
@foreach($data as $data)
<div class="row sm-6 ml-4 mb-1" class="no-gutters" style="height:25px; width: auto;">
<p class='text-dark mr-2'><input type="checkbox" name="prod_name[]" value="{{$data->title}}" class="products product{{$data}}" onClick="checkTest()"/> {{$data->title}}</p>
<p class='text-dark'>Qty:</p><input style="width:80px; height:25px;" type="number" name="prod_qty[{{$data->title}}]" min="1" value="1" class="form-control ml-2">
<input type="hidden" name="product_fee[{{$data->title}}]" value="{{$data->price}}">
<input type="hidden" name="prod_id[{{$data->title}}]" value="{{$data->id}}">
</div>
@endforeach
</div>
<div class=" col-lg-12 mt-5">
<fieldset>
<button name="submit" type="submit" id="form-submit" class="main-button-icon">Make A Reservation</button>
</fieldset>
</div>
</div>
</form>
<script>
var minimumonechecked;
var checkboxes = $('.products').lenght;
function checkTest(xyz){
minimumonechecked = false;
for(i=0;i<checkboxes;i++){
if($('product' + i).is(':checked')){
minimumonechecked = true;
}
}
console.log(minimumonechecked)
};
</script>
This is also the code in my controller, other data such as names are also part of the form but I cut it out by focusing on the checkbox
public function reservation(Request $request)
{
if(Auth::id()){
$user_id=Auth::id();
$products = '';
$reserved_qty=0;
$product_fee=0;
$prod_id=0;
$checked_array = $request->input('prod_name', []);
$quantities = $request->input('prod_qty', []);
$fees = $request->input('product_fee', []);
$productid = $request->input('prod_id', []);
foreach($checked_array as $value){
$data = new reservation;
$data->user_id=$user_id;
$data->name=$request->name;
$data->email=$request->email;
$data->phone=$request->phone;
$data->address=$request->address;
$data->date=$request->date;
$data->time=$request->time;
$data->status="pending";
$data->productz=$request->products="{$value}";
$data->reserved_qty=$request->$reserved_qty="{$quantities[$value]}";
$data->product_fee=$request->$product_fee=$fees[$value]*$quantities[$value];
$data->prod_id=$request->$prod_id=$productid[$value];
$data->save();
}
return redirect()->back();
}else{
return redirect('/login');
}
2
Answers
When you tick on checkbox, you need to count all checkboxes that have been checked before.
If at least checkbox is checked , you’ll set
disabled
property on submit button istrue
and vice verseThere are a many ways to achieve your desired result, how I would approach it is as follows (note this is not exhaustive, it’s a functional example).
On the client, use JavaScript to select all your
product
checkboxes, iterate over any found and attach event handlers to each of them which listen for thechanged
event (i.e.checked
andunchecked
). When the state of acheckbox
ischecked
, add it to aSet
so that we can track how many are selected. Based on the number ofchecked
checkboxes, enable/disable form submission.That theory out of the way, some code:
HTML:
JavaScript:
Example CodePen
Now lets apply the above to a Laravel example using
@foreach
andEloquent
models. I am going to assume you already have aProduct
model, migration and some data seeded.Form HTML:
As we all know you shouldn’t rely soley upon client side validation, so you’ll want to do some server side validation too. Once you’ve validated your request data, you can do whatever you want with it (I just get the products from the database in this example). I have put the code directly in a
Route
withinweb.php
as I am lazy, you’d obviously use it in yourProductController
function.routes/web.php:
Example PHP Sandbox
Quite a lot to taken in but hopefully that makes sense.