1

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" >
                    {{ csrf_field() }}
                    <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=\"{{$key}}\">{{$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 = "{{route('email-template.store')}}"; // 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 = "{{route('email-template.index')}}";
                                   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

2 Answers 2

1

One way to get around this is to count either the email_subject or email_body fields and then use that value for the min validation rule for the email_type:

$count = count($this->input('email_subject', []));

return [
    'email_subject'   => 'required|array',
    'email_subject.*' => 'required|string',
    'email_body'      => 'required|array',
    'email_body.*'    => 'required|string',
    'email_type'      => "required|array|min:{$count}",
    'email_type.*'    => 'required|array',
    'email_type.*.*'  => 'required|string',
];
Sign up to request clarification or add additional context in comments.

1 Comment

@BishwaRanjanTimilsina Glad I could help!
1

Use min:1. It will check the array must have minimum number of values.

 return [
            'email_subject.*' => 'required|string',
            'email_body.*' => 'required|string',
            'email_type' => 'min:1', 
        ];

3 Comments

'email_type' => 'required' already works in a single form but in multiple form(Note: Another same form added after clicking add another form ) is not working two individual checkbox group of different form. I have require at least one checkbox from a group checkbox of individual multiple form.
In your case I think it should be "email_type.*" => 'min:1' since each email_type entry is an array of the user's options (right?)
you enable multiple with min like min:2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.