function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CloudMikeCloudMike 

Validation Error: Value is not valid - One Cause/Solution

I recently ran into a problem where a Visualforce page was throwing a cryptic error message related to a custom picklist (apex:selectList).  The complete error was something like:

 

j_id0:frmSelectProductsServicesw:filterSection:j_id10:j_id15:selectedPF:Validation Error: Value is not valid

 

After some digging and research here in the forums, testing and a call to Tech Support it seemed the cause and solution for similar problems didn't quite apply.  I eventually found the solution (in my case) and wanted to share it in hopes that it would help others.

 

The cause was due to a conflict between the underlying stored data in SFDC and how certain controls behave when rendered to HTML.  In this case, there was a double space embedded in the underlying data between the Z and I in the value Product.Family field (eg. ‘S&U Z  Internal’). 

 

Background:

  1. For Visualforce controls (ie. apex.outputField, apex.outputText, apex:selectList) and modes (ie. edit and view), every control ultimately must renders as HTML.  Typical HTML rendering behavior suppresses any extra spaces within text.  That is why it is common practice to use the &nbsp to chain multiple spaces together. 
  2. In this case, the underlying stored data in the Product.Family field included a double space:
          ‘S&U Z  Internal’
    however when it was rendered to the screen, the HTML took control and used only a single space:
          ‘S&U Z Internal’
  3. Once the user clicked the submit button to begin the search, the picklist in this case tried to cross-reference the selected value with the actual data that was bound to the control and found no match, thus the cryptic error that was returned - “selectedPF:Validation Error: Value is not valid”.
          ‘S&U Z  Internal’ != ‘S&U Z Internal’

 

Funny thing is, when the underlying data is rendered in a input textbox (ie. apex.inputText), the double space is displayed.  Once I removed the double space from the underlying data, the error was fixed.

 

Hopefully this solution helps.

Thanks to Sreenivas Voore in Premiere Tech Support.

 

Best Answer chosen by Admin (Salesforce Developers) 
jwetzlerjwetzler

Thanks for the information Mike.  This error message tends to give people a lot of troubles and this is a facet of it that I have not heard yet.  The message is admittedly not very clear and we should probably at least add some more detail to it.

 

It's not obvious, but when you submit a form with a selectList in it, the getter for your selectOptions is called again and the value that's submitted to the server is checked against the list of options to make sure that it's valid.  We've gone back and forth on whether this level of validation is really necessary.  The one way that it would be useful is if you are selecting from a list of options that could be constantly changing (say, a list of open opportunities, that could be marked as closed in between the time from when you open your page and submit your form).  It might be a little excessive and could be implemented by the user if they really wanted that.  I've been thinking for a while that we should remove that validation altogether.

 

Usually when we see this error it's because someone has written their selectOptions getter in a way that will not produce consistent results, and therefore on postback they wind up with either a different set of results or an empty list.

 

I'll talk with my PM about this again.  Thanks for contributing your findings to the forums, hopefully this thread will help someone who has been facing a similar issue.

All Answers

jwetzlerjwetzler

Thanks for the information Mike.  This error message tends to give people a lot of troubles and this is a facet of it that I have not heard yet.  The message is admittedly not very clear and we should probably at least add some more detail to it.

 

It's not obvious, but when you submit a form with a selectList in it, the getter for your selectOptions is called again and the value that's submitted to the server is checked against the list of options to make sure that it's valid.  We've gone back and forth on whether this level of validation is really necessary.  The one way that it would be useful is if you are selecting from a list of options that could be constantly changing (say, a list of open opportunities, that could be marked as closed in between the time from when you open your page and submit your form).  It might be a little excessive and could be implemented by the user if they really wanted that.  I've been thinking for a while that we should remove that validation altogether.

 

Usually when we see this error it's because someone has written their selectOptions getter in a way that will not produce consistent results, and therefore on postback they wind up with either a different set of results or an empty list.

 

I'll talk with my PM about this again.  Thanks for contributing your findings to the forums, hopefully this thread will help someone who has been facing a similar issue.

This was selected as the best answer
CloudMikeCloudMike

Thanks Jill.  Appreciate the insight into the behind the scenes behavior.  I'd be interested to see if it does change as you have indicated.

 

 

aamDevaamDev

Hi Jill,

 

Count me in with those running across this issue. My VF page uses some javascript to pass values between 2 selectLists ala dueling list box. Each selectOption getter is populated with a loop on a certain object.  If I'm reading your post correctly, as long as my users move values, I will always receive this validation error. I've looked over the various forums, asked assistance from other posters and have yet to find a solution. As I see your name in many related posts, any help you could give would be greatly appreciated. Thanks!

 

Adriel

mikefitzmikefitz

Any update to this issue? Work arounds?

 

I  have one static picklist and another that is dependent on the value of the first. Initially it works but when the first picklist changes I continue to run into this error.

 

Thanks

 

Mike

 

 

MJ09MJ09

Count me in as someone who has beat her head against the wall trying to figure out why I'm getting this message. I just spent several hours on what turned out to be two separate problems on the same page - for one select list, it was related to getter/setter behavior, and for another, it was related to a SelectOption that had 2 spaces in the label.

 

Many thanks to SatelliteMike for his posting about the double-spaces - that was huge!

 

Jill, your comments in several discussion board posts on why the select list's getter is called when the form is submitted are helpful, and it'd be even more helpful if you updated the VF Developer's Guide to describe that behavior, the error message, and how to avoid the problem. A blog posting might also help.

 

 

sfdcfoxsfdcfox

I've experienced it previously myself, and I'd forgotten it again (it is certainly an obscure glitch). Thanks for reminding me on this one, because we can all use little reminders about things like that. When possible, try using some sort of record ID or a "safe" string (i.e. an index number or a String.replace() fixed string) for the "value", and translate that value in the controller. It certainly is safer.

 

In some scenarios, a system-forced validation isn't a bad thing (it appears that all the open-source CMS solutions like Drupal do something like this); I could certainly see a use for this validation on public-facing forms (unauthenticated) to avoid "hacking" in POST data with garbage, as well as reducing the developer's burden on validating more input than necessary (imagine having to validate apex:inputField values to make sure that users entered a valid number for Opportunity.Amount each time). That being said, I would propose a "toggle" option so developers can choose to have this sort of validation kick in, or allow the developer an option to allow this function to be disabled (because of the aforementioned possible issues).

 

Also, maybe you could look into automatically escaping whitespace as   to preserve spaces?

 

At any rate, it's great to know that a PM is aware of the problem; I thought I'd just throw in my two cents as a fellow developer and community member.

Karimov CeyhunKarimov Ceyhun

Hi,

 

I have experienced the same problem. I think there is a little bug . The problem arrises when we try to write the list of selectoption to the variable in controller.   The point is that, when we select mutiselect=true, we must read values from variable of type list<selectoption>  and write values to variable of type list<string>.

 

for example.

 

apex:selectList id="selectedSelectList"  value="{!selectedSelectList}" multiselect="true" style="width:180px" size="10">
                        <apex:selectOptions value="{!availableSelectList}"/>
                    </apex:selectList>

here availablelist is of type list<selectoption> and selectedselectlist is of type list<string>.

 

 

Thanks.

 

Ceyhun Karimov

WesNolte__cWesNolte__c

Great post. I found a quick solution to my problem because of the information here. It looks like you might get this error if you declare your selectoptions as "Transient" so take care.

 

Wes

Karimov CeyhunKarimov Ceyhun

No the problem is, if you change the picklist elements by javascript, you will get this error.I think this is a (bug) / (security action)  by salesforce.

 

 

 

Thanks

 

Ceyhun Karimov

WesNolte__cWesNolte__c

Yeah you're right, that is one cause of this error. I've describe another potential cause.

Karimov CeyhunKarimov Ceyhun

Thanks for your reply,

 

However there is a workaround for this issue, you can do all this stuf by javascript and hidden input fields.You just play with selectoptions just for visual purposes but at every click you do operations by javascript and add remove some string to hidden input field.And at the end, you send hidden input to contoller and do some operations.

 

 

 

Thanks

 

Ceyhun Karimov

WesNolte__cWesNolte__c

I think you're misunderstanding the reason I was posting. I agree with everything that was in post before I got there, and I agree with you. All I was saying is that there is more than one thing that can cause this problem. In my case it was:

 

transient List<SelectOption> options = ....

 

This is 1 more reason that this error can occur. I was not replying directly to you in the post, I was replying generally.

 

Wes

Karimov CeyhunKarimov Ceyhun

 

Sorry for misunderstanding,

 

Thanks

 

Ceyhun Karimov

sfdcfoxsfdcfox

Yes, when a form is submitted, a number of checks are run, including checking all selected picklist values against all possible values for those picklists, which may be any combination of hardcoded entries and dynamically generated entries (such as from a variable or a getter method). This is done before calling any setters but after restoring the page state. Using transient variables can cause this as well as non-determininstic functions (such as making picklist values from random numbers in a getter).

CloudMikeCloudMike

I came across this issue a while ago.  Glad to see that others continue to find the info on this issue helpful!  Thanks also to the additional feedback and insight on similar scenarios that can cause the problem!  

Di TiempoDi Tiempo

Hey guys, I have 2 related questions:
-When is this validation being executed?  Before the method refered to in the action tag of a submit button?
-Is it possible to catch this error? I tried with try-catch but that won't work. Is that because the validation is executed before the method?

Thanks in advance

sfdcfoxsfdcfox

-When is this validation being executed?  Before the method refered to in the action tag of a submit button?

 

- The validation occurs after deserialization, form processing, and getters, before calling any non-idempotent functions (classified as setters and action methods). Getter methods must be idempotent in regards to picklist values, or it may cause this validation exception to occur.

 

-Is it possible to catch this error? I tried with try-catch but that won't work. Is that because the validation is executed before the method?

 

- You cannot catch this error, because it occurs at the system level, before your code has a chance to execute the action function; this is the same basic priciple as a desktop application when an anti-virus program determines it is a threat (e.g. the program will never detect the fact that it didn't run). There has been talk here on the forums and elsewhere about salesforce.com having internal discussion about removing this "safety net" (or allowing it to be optionally disabled per page) to allow the developer to choose their risk level versus dynamic benefits. For example, developers have wanted to generate a list of values dynamically in JavaScript, possibly from an external system, but this only works today if the list is generated in Apex Code, or you produce some significantly advanced hacks in JavaScript.

FlippsieFlippsie

I had a bizarre encounter with this issue.  I picked up some code of another developer who had implemented the hidden text field work-around, (Concatenating all the selected items into a comma delimeted string) but the server-side validation was failing in Opera - all other browsers were OK.  I had to ensure that, after the items were concatenated into the hidden text field, I had removed all the selected items from the list.  So there was nothing left to fail the validation.

 

Very odd, apparently browser specific service-side validation!

Charles de BrabandereCharles de Brabandere
Hi,

If you can't modify the data, you can manipulate the string containing the double spaces during the creation of the list in the controller...

String str = ' how is   the weather today ';
str = str.trim();
str = str.replaceAll('(\\s+)', ' ');

// str ==  'how is the weather today'



Faiza NazFaiza Naz

Hi All,

I am also running into the same issue my visulaforce page through error.

Page:form:pb1:pbs1:pbt1:0:OperatorList: Validation Error: Value is not valid.

I am not sure how to resolve it.As  I am using List<selectoption> to bind selectoption,moreover there are no space in my list values.

I am sharing my code for the refrence.Any kind of help is appreciated.

VF Page:
<apex:selectList size="1" id="OperatorList" disabled="{!IsDisabled}" value="{!item.selectedOperator}">
<apex:selectOptions value="{!item.Operator}" />
</apex:selectList >

Controller:
//get Operators from function 
    public List<SelectOption> getRelatedOperators(string strtype){
        
        List<selectOption> Operators = new List<selectoption>();
        Operators = Utility.GetOperator(strtype);
    
        return Operators;
    }

Utility:
public static List<SelectOption> GetOperator(String Type){
ListOfOperator.add(new SelectOption('=','EQUAL'));
return ListOfOperator;
}
Didier BeaufaysDidier Beaufays
I also encountered this issue and searched for hours about the cause because the error was not exposed directly in the debug logs, I saw it by chance with an Apex page error message.
In my case a SelectOptions list contained key and value with probably some bad characters. When the picklist element was rerendered in the page, some elements with special characters ( accents, spaces, ... I'm not sure) caused the page form sumbission fail.

To resolve it I cleaned the keys of the Map to remove all non ascii chars and spaces or used Ids instead when available.
Thad DahlbergThad Dahlberg
I was getting this same error too. I was building a picklist off of a salesforce textarea field split on new lines (\n). I was using the same method and the same data on the initial view and the post back.

It turned out there were hidden characters in the picklist values I was generating. When using the inspector in chrome you could see a line break but not in the raw code view. I tried removing extra \n in the value and this did not work. In the end I used a regex to strip all values out of my string that were not letters, numbers, spaces and a few punctuations. After cleaning the string values for the pick list things worked! No more error.

Here is the method I ended up using:
public List<SelectOption> createPicklists(String returnSepStringList) {
        if (String.isBlank(returnSepStringList)) {
            return null;
        }
        List<SelectOption> cpl = new List<SelectOption>();
        cpl.add(new SelectOption('', 'Select...'));
        returnSepStringList = returnSepStringList.Trim();
        returnSepStringList = returnSepStringList.replace('\n\n', '\n');
        String[] splitList = returnSepStringList.split('\n');
        for (String p : splitList) {
            p = p.replaceAll('[^a-zA-Z&!. ]', '');
            cpl.add(new SelectOption(p, p));
        }
        return cpl;
    }
returnSepStringList is a textarea field value I set in an object in salesforce. It generates a picklist on ne lines like this:
Red
Blue
Green
Yellow
The code that fixed this error was this:
p = p.replaceAll('[^a-zA-Z&!. ]', '');
It trimmed all the hidden characters from the string (though in theroy I have no idea why they were there).

I created the picklist in the controller by calling the method into a List<SelectOption> variable (Question_1_Picklsit__c being a salesforce field with a list of values seperated by a return):
picklist1 = createPicklists(ss.Question_1_Picklist__c);
In visual force here is how I generated the list:
<apex:selectList value="{!ssRegistration.Answer_1__c}" size="1" required="true" id="picklistOne">
         <apex:selectOptions value="{!picklist1}"/>
</apex:selectList>
Hope this helps someone.
Shaleen Yadav 6Shaleen Yadav 6
I was able to resolve it for my case. I was using multiple selectLists on the vf page, the get methods were constructing a list of values from a query. When I removed the database.query from get methods to constructor it resolved the issue. My guess is having query inside get method allows selectlist to change values and get caught in this system error.
bobnunny1bobnunny1
Thought I'd add another cause/solution to this.  In my case, it was because the LIST holding the options was "static" and not an instance get/set.  Once I removed static, it worked properly.