Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Checkbox group #143

Closed
mmiraglia opened this issue Jul 13, 2016 · 2 comments
Closed

Checkbox group #143

mmiraglia opened this issue Jul 13, 2016 · 2 comments

Comments

@mmiraglia
Copy link

Hi!
I've 3 checkboxes with the same name ( destiny[] ) and I need to verify that at least one is selected.
Any idea?
Thx!!!

@ansman
Copy link
Owner

ansman commented Jul 25, 2016

Currently this isn't supported, but you can follow the progress in #1

@ansman ansman closed this as completed Jul 25, 2016
@c0d3sling3r
Copy link

c0d3sling3r commented Sep 30, 2020

I have faced this issue before and made an effort which is followed and it works.

validate.validators.checkboxList = (value, options, key, attributes) => {
    if (value.length === 0) return options.message;

    var validation = true;

    $.each(value, function (index, item) {
        if (item !== "") {
            validation = options.within.includes(item);
            if (validation === false)
                return;
        }
    });

    if (validation)
        return null;
    else
        return options.message;
}

Note 1: You have to prepare your value in an array. i.e. If you have three different checkboxes which are holding 1,2 and 3 as value, your final value which should be passed to validation component should be something like this: Array [1,3].

Note 2: My checkboxList component accepts an option in following schema:

"validationKey": {
    checkboxList: {
      within: ["1", "2", "3"],
      message: "Your invalid warning message"
    }
}

And FYI, I have written a piece of other codes to prepare my component's food 😅.

function prepareCheckboxListValue(selector) {
    var chkbxs = $(selector);
    var value = [];

    $.each(chkbxs, function (index, item) {
        if ($(item).prop("checked"))
            value.push($(item).val());
    })

    return value;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants