Jama A.
Jama A.

Reputation: 16079

I'm getting "Value is not a valid option" for SelectManyCheckBox

I have this code in my JSP page:

<h:selectManyCheckbox id="chb" value="#{MyBean.selectedCheckBoxes}" layout="pageDirection">
     <f:selectItems value="#{MyBean.checkBoxItems}"/>
</h:selectManyCheckbox>

And in my MyBean:

public class MyBean {

    public MyBean() {
        for (Elem section : sections) {
            checkBoxItems.put(section.getName(), section.getObjectID());
        }
    }

    private String[] selectedCheckBoxes;
    private Map<String, Object> checkBoxItems = new LinkedHashMap<String, Object>();

    public String save() {
        //save is not being executed....
        return FORWARD;
    }

    public Map<String, Object> getCheckBoxItems() {
        return checkBoxItems;
    }

    public void setCheckBoxItems(Map<String, Object> checkBoxItems) {
        this.checkBoxItems = checkBoxItems;
    }

    public String[] getSelectedCheckBoxes() {
        return selectedCheckBoxes;
    }

    public void setSelectedCheckBoxes(String[] selectedCheckBoxes) {
        this.selectedCheckBoxes = selectedCheckBoxes;
    }
}

When I click save it is giving the below message in <t:message for="chb"/>

"chb": Value is not a valid option.

Even though I did not add the required attribute for h:selectManyCheckbox, it is trying to validate or doing something else... I've changed checkBoxItems variable type(with getter/setters) to List<SelectItem>, but it is not working as well. What can be the reason, how can I solve it?
PS: I'm using JSF 1.1

Upvotes: 1

Views: 2847

Answers (2)

BalusC
BalusC

Reputation: 1108632

You will get this error when the equals() test on a selected item has not returned true for any of the available items. So, when roughly the following happens under JSF's covers:

boolean valid = false;

for (Object availableItem : availableItems) {
    if (selectedItem.equals(availableItem)) {
        valid = true;
        break;
    }
}

if (!valid) {
    // Validation error: Value is not valid!
}

That can in your particular case only mean that section.getObjectID() does not return a String which is what your selectedCheckboxes is declared to, but a different type or a custom type where equals() is not implemented or broken.


Update as per your comment, the getObjectID() returns Integer. It's thus been treated as String because selectedCheckBoxes is declared as String[]. You should change the following

private String[] selectedCheckBoxes;
private Map<String, Object> checkBoxItems = new LinkedHashMap<String, Object>();

to

private Integer[] selectedCheckBoxes;
private Map<String, Integer> checkBoxItems = new LinkedHashMap<String, Integer>();

and maybe (not sure, can't tell from top of head now) also explicitly supply a converter:

<h:selectManyCheckbox ... converter="javax.faces.Integer">

Upvotes: 3

sadia
sadia

Reputation: 325

i didnt find any problem in th code, i thought there is the problem the list u passed to oneManyCheckBox.

hardcode some values in list in getter than check

  public Map<String, Object> getCheckBoxItems() {

    checkBoxItems.clear();
    checkBoxItems.put("aaaa", "aaaa");
    checkBoxItems.put("bbbb", "bbbb");
    checkBoxItems.put("cccc", "cccc");
    checkBoxItems.put("dddd", "dddd");
    checkBoxItems.put("eeee", "eeee");

    return checkBoxItems;
   }

Upvotes: 0

Related Questions