Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8934107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:42:26+00:00 2026-06-15T09:42:26+00:00

I got j_idt7:city: Validation Error: Value is not valid Cutting to the cheese, ManagedBean

  • 0

I got

j_idt7:city: Validation Error: Value is not valid

Cutting to the cheese, ManagedBean code:

 //few imports here
 @ManagedBean
 @SessionScoped
 public class CountriesAndCities implements Serializable{
 private List<SelectItem> countries;
 private List<SelectItem> cities;
 private  Map<String,List> m;
 private String selectedCountry;

 public String getSelectedCountry() {
return selectedCountry;
 }

 public void setSelectedCountry(String selectedCountry) {
this.selectedCountry = selectedCountry;
 }

 public CountriesAndCities(){
countries = new ArrayList<SelectItem>();
cities = new ArrayList<SelectItem>();
m = new HashMap<String,List>();
m.put("France", Arrays.asList("paris","marseille"));
m.put("England", Arrays.asList("Munchester","liverpoor"));

 }

 public  List<SelectItem> getCountries(){   
cities.removeAll(cities);
countries.removeAll(countries); 
countries.add(new SelectItem("select country"));
for(Map.Entry<String, List> entry: m.entrySet()){
    countries.add(new SelectItem(entry.getKey()));
    }

return countries;
 }
 public List<SelectItem> getCities(){   
for(Map.Entry<String, List> entry: m.entrySet())
      {if(entry.getKey().toString().equals(selectedCountry)){
        cities.addAll(entry.getValue());
        break;
    }
}
return cities;
 }
 public void checkSelectedCountry(ValueChangeEvent event){
selectedCountry = event.getNewValue().toString();   
 }

Here’s the snippet of my .xhtml :

 <h:selectOneMenu immediate="true" value="#{countriesAndCities.selectedCountry}" 
 onchange="submit()" valueChangeListener="#{countriesAndCities.checkSelectedCountry}">
 <f:selectItems value="#{countriesAndCities.countries}"></f:selectItems>
 </h:selectOneMenu>
 <br/>
 <h:selectOneMenu id="city">
 <f:selectItems value="#{countriesAndCities.cities}"></f:selectItems>
 </h:selectOneMenu>     
 </h:form>

The code does what is supposed to be, But I get the error mentioned above at first line, only when i click on England and select country choices, I dunno why, I’ve written the same task in Ajaxized code, and It worked fine, any hand would be dead thankful .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:42:27+00:00Added an answer on June 15, 2026 at 9:42 am

    The error Validation Error: Value is not valid will be thrown when the equals() test of the selected item has not returned true for any of the available items in the list. So, it has basically the following two causes:

    1. The equals() method of the item value type is missing or broken.
    2. The list of available items has incompatibly changed during the submit.

    The item value type is String, so its equals() method is undoubtedly properly implemented (else it would break everything in Java world). So cause 1 can be scratched. Left over cause 2. Yes, indeed, you’re emptying the list of available cities in the getter of countries for some unclear reason.

    public List<SelectItem> getCountries() {   
        cities.removeAll(cities);
        // ...
    }
    

    This code just doesn’t make sense. The getter is also called when the selected country is to be validated. This way the selected city can’t be found in the list of available cities when it’s about to be validated later on. By the way, your getCities() also doesn’t make any sense. You’re looping over the whole map instead of just using Map#get().

    This all is just broken code design. Getters should not contain any business logic at all. Getters should solely return bean properties. Those bean properties should already be preinitialized long before in (post)constructor or in action(listener) method.

    How to fix this is a good second problem. The <h:selectOneMenu valueChangeListener> is basically abused here. It’s the wrong tool for the job. You should be using <f:ajax listener> for this instead. But if you really insist in abusing the valueChangeListener for the job, then you should have solved it as follows, whereby you queue the ValueChangeEvent to INVOKE_APPLICATION phase and perform the job during that phase (as if it were an (ajax) action listener method):

    private List<String> countries;
    private List<String> cities;
    private Map<String, List<String>> countriesAndCities;
    private String selectedCountry;
    
    @PostConstruct
    public void init() {
        countriesAndCities = new LinkedHashMap<String, List<String>>(); // Note: LinkedHashMap remembers insertion order, HashMap not.
        countriesAndCities.put("France", Arrays.asList("paris","marseille"));
        countriesAndCities.put("England", Arrays.asList("Munchester","liverpoor"));
        countries = new ArrayList<String>(countriesAndCities.keySet());
    }
    
    public void checkSelectedCountry(ValueChangeEvent event) { // I'd rename that method name to loadCities() or something more sensible.
        if (event.getPhaseId() != PhaseId.INVOKE_APPPLICATION) {
            // Move the job to the INVOKE_APPLICATION phase. 
            // The PROCESS_VALIDATIONS phase is the wrong phase for the "action listener"-like job.
            event.setPhaseId(PhaseId.INVOKE_APPLICATION);
            event.queue();
            return;
        }
    
        cities = countriesAndCities.get(selectedCountry);
    }
    
    // Getters and setters. Do not change them! They should just get and set properties. Nothing more!
    
    public List<String> getCountries(){   
        return countries;
    }
    
    public List<String> getCities(){   
        return cities;
    }
    
    public String getSelectedCountry() {
        return selectedCountry;
    }
    
    public void setSelectedCountry(String selectedCountry) {
        this.selectedCountry = selectedCountry;
    }
    

    Note that this still causes potential problems when you have some form validation around. A much better solution is to just use <f:ajax> instead.

    See also:

    • Our selectOneMenu wiki page – at the bottom you can find a proper <f:ajax> example.
    • Validation Error: Value is not valid
    • When to use valueChangeListener or f:ajax listener?
    • Why JSF calls getters multiple times
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At Run Time I got This Error pet is not mapped.. In My Following
Got a few problem here. I have created a arrays of EditText and it
got a few question here. i just want to get the data from the
I got an unaligned memory accesses not supported error and did a Google search
I got an error message from my code which says TypeError: 'int' object is
I've got the following simple code: @for (int j = 0; j < file.Items.Count;
Got this error message while trying to load view: The model item passed into
I got NameError when I try to run this codes.global name j is not
I got a compiling error when using std::copy to convert a 1D vector to
I've got this code and I don't know why all the functions in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.