dimanche 3 mai 2020

Validation of at least one checkbox from group in multiple forms in laravel

enter image description here I have groups of checkbox and multiple forms. At least one checkbox from a group of a form should be checked. One or more than one form can exist. I have searched but can not find an answer from google. Please help me to solve this problem. I have following code of create.blade.php

@extends('layouts.backend.app')

@section('title', 'Dashboard - Create Email Templates')

    @section('content')
        <div class="col-lg-12">

            <h1 class="page-header">Create Email Templates</h1>

        </div>

            <div class="col-lg-8">
                <p> <strong>Note :</strong> Fields given <span class="text-info">*</span> are Compulsory.</p>
                <form method="post" id="foo" >
                    
                    <div class="multiple_feilds">
                    </div>
                    <div class="form-group">
                        <a id="add_another_form">Add Another Form</a>
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>

            <script type="text/javascript">
                $(document).ready(function(){

                    function createForm(n){
                        var form = ""+
                            "<div class=\"form\">"+
                                "<div class=\"form-group\">"+
                                    "<label class=\"text-primary\">#Form "+n+"</label>"+
                                "</div>"+
                                "<div class=\"form-group\">"+
                                    "<label>Email Subject<span class=\"text-info\">*</span></label>"+
                                    "<input class=\"form-control\" type=\"text\" name=\"email_subject[]\">"+
                                    "<span class=\"email_subject text-danger\"></span>"+
                                "</div>"+
                                "<div class=\"form-group\">"+
                                    "<label>Email Body<span class=\"text-info\">*</span></label>"+
                                    "<textarea class=\"form-control\" name=\"email_body[]\"></textarea>"+
                                    "<span class=\"email_body text-danger\"></span>"+
                                "</div>"+
                                "<div class=\"form-group\">"+
                                    "<label>Email Type<span class=\"text-info\">*</span></label>"+
                                    @php
                                        $emailType = ['register_email'=>'Register Email','weekly_email'=>'Weekly Email','other'=>'Other'];
                                    @endphp
                                    @foreach($emailType as $key => $value)
                                    "<div class=\"checkbox\">"+
                                        "<label>"+
                                            "<input type=\"checkbox\" name=\"email_type["+n+"][]\" value=\"\">"+
                                        "</label>"+
                                    "</div>"+
                                    @endforeach
                                    "<span class=\"email_type text-danger\"></span>"+
                                "</div>"+
                                "<div class=\"form-group\">"+
                                    "<input type=\"hidden\" name=\"form_id[]\" value=\""+n+"\">"+
                                "</div>"+
                                "<a class=\"remove_form\">Remove Form</a>"+
                                "<hr class=\"multiple_form_separator\">"+
                            "</div>";

                        return form;

                    }

                    var n = 1;

                    var form = createForm(n);

                    $(".multiple_feilds").html(form);

                    $("#add_another_form").click(function(){

                        n = n+1;

                        var form = createForm(n);

                        $('.multiple_feilds').append(form);

                    });

                    $('.multiple_feilds').delegate('.remove_form', 'click', function () {

                        $(this).parent().remove();

                    });

                });

                //Ajax Validation For Laravel
                /* attach a submit handler to the form */
                $("#foo").submit(function(event) {

                alert('validating');

                /* stop form from submitting normally */
                event.preventDefault();

                /*clear result div*/
                $("#result").html('');

                /* get some values from elements on the page: */
                var values = $(this).serialize();


                 var url = ""; // the script where you handle the form input.

                 $.ajax({
                           type: "POST",
                           url: url,
                           data: $("#foo").serialize(), // serializes the form's elements.
                           success: function(data)
                               {
                                   indexPageUrl = "";
                                   location.href = indexPageUrl;
                               },
                            error:function(msg){
                                data = msg.responseJSON.errors;
                                console.log(data);
                                var email_subject=document.getElementsByName("email_subject[]");
                                for(var x=0;x<email_subject.length;x++){
                                    if( typeof data["email_subject."+x]!=="undefined"){
                                        document.getElementsByClassName("email_subject")[x].innerHTML = data["email_subject."+x];
                                    }else{
                                        document.getElementsByClassName("email_subject")[x].innerHTML = "";
                                    }
                                }


                                var email_body=document.getElementsByName("email_body[]");
                                for(var x=0;x<email_body.length;x++){
                                    if( typeof data["email_body."+x]!=="undefined"){
                                        document.getElementsByClassName("email_body")[x].innerHTML = data["email_body."+x];
                                    }else{
                                        document.getElementsByClassName("email_body")[x].innerHTML = "";
                                    }
                                }

                                // Validation of Group of Checkbox (At least one is required from a group.)

                            }
                         });

                    return false; // avoid to execute the actual submit of the form.


                });

            </script>
        </div>
    @endsection

And following code of FromRequest

           return [
                'email_subject.*' => 'required|string',
                'email_body.*' => 'required|string',
                'email_type' => 'required', //Please answer the validating rule
            ];

Please help me to find answer. Thanks in advance




Aucun commentaire:

Enregistrer un commentaire