Latest

Thursday, July 13, 2017

Validating selected checkboxes (array) with 'in'

Asked by: kjdion84


I have the following checkboxes in my form:

<div class="form-group">
    <label for="likes_turtles" class="col-md-3 control-label">Likes Turtles</label>
    <div class="col-md-6">
        <div class="checkbox">
            <label>
                <input type="checkbox" name="likes_turtles[]" value="Yes"> Yes
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" name="likes_turtles[]" value="No"> No
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input type="checkbox" name="likes_turtles[]" value="Maybe so"> Maybe so
            </label>
        </div>
    </div>
</div>

As you can see, they are an array of items because I want to allow selection of multiple items.

I am trying make it so that the checked items must be valid.

Here is my validation rule:

[likes_turtles] => required|in:Yes,No,Maybe so

The problem is, I'm getting the following error when I check any or all of these items:

The selected likes turtles is invalid.

How do I make laravel simply ensure any items in the request array likes_turtles belong to the specified list e.g. Yes,No,Maybe so



Source

No comments:

Post a Comment

Adbox