[SalesForce] Reg: Validation Error: value is not valid

I have a problem in my visualforce page, while rerendering the page, by clicking any button/ any request is sent to controller.

Visualforce page is:

apex:page controller="selectController">

<apex:pageblock >
<apex:form >
<apex:pagemessages />
<apex:repeat value="{!setOfSection}" var="section">

<br/>
<apex:pageBlocksection title="{section}">

<apex:variable value="{!mapOfSectionandItsClass[section]}" var="listOfInnerclass"/>

check:
<apex:repeat value="{!listOfInnerclass}" var="Innerclass"><br/>
<apex:selectList value="{!Innerclass.value}" multiselect="true" size="1">
<apex:selectoptions value="{!Innerclass.listOfdropdown}"/>

</apex:selectList>



</apex:repeat>


</apex:pageBlocksection>



</apex:repeat>


<apex:commandButton value="Check" action="{!check}"/>

</apex:form>
</apex:pageblock>

</apex:page>

Controller is:

public class selectController {

    public Map<String,List<InnerClass>> mapOfSectionAndItsClass{get;set;}
    public set<String> setOfSection{get;set;}

    public selectController(){
        mapOfSectionAndItsClass = new Map<String,List<Innerclass>>();
        setOfSection = new set<STring>();

        for(Innerclass__c newContact:[SELECT Name,Low__c,Medium__c,High__c,Section__c from Innerclass__c where section__c!=NULL]){
            List<InnerClass> listOfInnerClass = new List<InnerClass>();
            if(mapOfSectionAndItsClass.containsKey(newcontact.section__c)){
                listOfInnerClass = mapOfSectionAndItsClass.get(newContact.section__c);
            }
            Innerclass newInnerclass = new Innerclass();
            newInnerclass.listOfDropdown= new List<SelectOption>();
            newInnerclass.listOfDropdown.add(new SelectOption(newContact.Low__c,newContact.Low__c));
            newInnerclass.listOfDropdown.add(new SelectOption(newContact .Medium__c,newContact.Medium__c));
            newInnerclass.listOfDropdown.add(new SelectOption(newContact.High__c,newContact.High__c)); 
            listOfInnerClass.add(newInnerclass)                       ;
            mapOfSectionandItsClass.put(newContact.section__c,listOfInnerclass);            
        }

        setOfSection  = mapOfSectionandItsClass.keyset();
    }

    public pageReference check(){
        return NULL;
    }

    public class InnerClass{
        public String value{get;set;}
        public List<selectOption> listOfDropdown{get;set;}
    }

    public class Innerclass2{
        public string section{get;set;}
        public List<Innerclass> listOfInnerclasses{get;set;}
    }


}

Its loading fine with all the Innerclass and its options.

When i click the button, its throwing error as

(Id of the selectList):Validation error: value is not valid

This error is throwing other than last section.

could you please pin point the error in this page/classes, and help me to solve this..

Thanks..

Best Answer

The problem is that you are using Sets, which are unordered by nature. Since they are not reliably ordered, they may change around at any time, and if a submitted value doesn't match any of the select option values, an error will be raised. There's been some discussion about if select option values should be validated, or if it should be left to the developer to validate the values, but for now, the only workaround is to make certain that the order of the keys can't change. This error actually indicates a deeper problem, which is that if the select items were not validated, you would experience corrupted data in the controller.

This simple fix should resolve the problem:

public class selectController {

    public Map<String,List<InnerClass>> mapOfSectionAndItsClass{get;set;}
    // This is now a list
    public list<String> listOfSection{get;set;}

    public selectController(){
        mapOfSectionAndItsClass = new Map<String,List<Innerclass>>();
        listOfSection = new list<String>();

        for(Innerclass__c newContact:[SELECT Name,Low__c,Medium__c,High__c,Section__c from Innerclass__c where section__c!=NULL]){
            List<InnerClass> listOfInnerClass = new List<InnerClass>();
            if(mapOfSectionAndItsClass.containsKey(newcontact.section__c)){
                listOfInnerClass = mapOfSectionAndItsClass.get(newContact.section__c);
            }
            Innerclass newInnerclass = new Innerclass();
            newInnerclass.listOfDropdown= new List<SelectOption>();
            newInnerclass.listOfDropdown.add(new SelectOption(newContact.Low__c,newContact.Low__c));
            newInnerclass.listOfDropdown.add(new SelectOption(newContact .Medium__c,newContact.Medium__c));
            newInnerclass.listOfDropdown.add(new SelectOption(newContact.High__c,newContact.High__c)); 
            listOfInnerClass.add(newInnerclass)                       ;
            mapOfSectionandItsClass.put(newContact.section__c,listOfInnerclass);            
        }

        listOfSection.addAll(mapOfSectionandItsClass.keyset());
    }

    public pageReference check(){
        return NULL;
    }

    public class InnerClass{
        public String value{get;set;}
        public List<selectOption> listOfDropdown{get;set;}
    }

    public class Innerclass2{
        public string section{get;set;}
        public List<Innerclass> listOfInnerclasses{get;set;}
    }


}
Related Topic