vendredi 23 septembre 2022

How to validate at least one checkbox is checked in a loop in Laravel?

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="" 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="" class="products product" onClick="checkTest()"/> </p>
                    <p class='text-dark'>Qty:</p><input style="width:80px; height:25px;" type="number" name="prod_qty[]" min="1" value="1" class="form-control ml-2">
                    <input type="hidden" name="product_fee[]" value="">
                    <input type="hidden" name="prod_id[]" value="">
            </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');
        }



Aucun commentaire:

Enregistrer un commentaire