1

I have a collection of checkboxes in my application. What I want to do is validate whether at least one is checked and enable a submit button. Basically a front end validation. According to the current code, the user must select all the checkboxes. I know this should be a small issue but as I am new to Laravel framework it's a little bit confusing for me to understand. Please, someone, help me here.

@foreach ($roles as $role)
    <li>
        {{ Form::checkbox('roles[]', $role->id, null, ['class="role-li"', 'required']) }}
        {{ Form::label($role->name, ucfirst($role->name)) }}
    </li>
@endforeach
3
  • Normal validation should work for validating if at least one checkbox is check. Try with it by adding validation rule like 'roles' => 'required', Commented Jan 22, 2020 at 6:29
  • I have used this and validated it through the back end. But if the user didn't select a role it keeps submit button enabled and when submitting it displays the error message and not allowing to submit the data. I want to disable the button if the user didn't select any checkbox. I have required other Form fields and it is working fine. But here it expects the user to select all the checkboxes. I want to make it for at least one. How can I do that? Commented Jan 22, 2020 at 6:34
  • Want to require at least one checkbox as required other fields in this question: stackoverflow.com/questions/24405524/… Commented Jan 22, 2020 at 6:41

2 Answers 2

1
<form id="testform" action="/comment" method="post" class="m-0 mt-4">
    @for($i=0;$i<5;$i++)
        <input type="checkbox" name="vehicle1" value="Bike" class="role-li role{{$i}}" onclick='checktest()'> I have a bike<br>
    @endfor
</form>

<script>
        var minimumonechecked;
        var noofboxes = $('.role-li').length;
        function checktest(xyz) {
            minimumonechecked = false;
            for(i=0;i<noofboxes;i++){
                if ($('.role' + i).is(':checked')) {
                    minimumonechecked = true;
                }
            }
            console.log(minimumonechecked)
        };
</script>

this code works for me :)

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer. But bad luck it is not working.
can you check step by step and check where the code is wrong?
syntax error, unexpected '}', expecting ',' or ')' (View: C:\xampp\htdocs\lara-club-1\resources\views\users\user-create.blade.php) this is the error message. And the error is in {{ Form::checkbox('roles[]', $role->id, null, ['class="role-li role{{$key}}"', 'required']) }} this line of code.
'class' => 'role-li role{{$key}}' or 'class' => 'role-li , role{{$key}}' one of this should work
okay, just check how can u give multiple classes to that checkbox, once that is sorted, try my script
|
1

Try this

var checkboxes = $(".check-cls"),
    submitButt = $(".submit-btn");

checkboxes.click(function() {
    submitButt.attr("disabled", !checkboxes.is(":checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Button should be enabled if at least one checkbox is checked</h1>

<form>
    <div>
        <input type="checkbox" name="roles[]" id="role-1" class="check-cls"> <label for="role-1">Role 1</label>
    </div>
    <div>
        <input type="checkbox" name="roles[]" id="role-2" class="check-cls"> <label for="role-2">Role 2</label>
    </div>
    <div>
        <input type="checkbox" name="roles[]" id="role-3" class="check-cls"> <label for="role-3">Role 3</label>
    </div>
    <div>
        <input type="checkbox" name="roles[]" id="role-4" class="check-cls"> <label for="role-4">Role 4</label>
    </div>
    <div>
        <input type="checkbox" name="roles[]" id="role-5" class="check-cls"> <label for="role-5">Role 5</label>
    </div>

    <div>
        <input type="submit" class="submit-btn" value="Submit" disabled>
    </div>
</form>

jsfiddle

Comments

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.