1

I got this error many time.. i am using two h:selectonemenu in my JSF page, mediaList and Unitlist. while selecting any Media . my UnitList populate automatically, but some time it gives Validation Error: value is not valid;

My JSF code

<h:selectOneMenu  id="media" value="#{workOrderMbean.selectedMedia}" converter="MediaConverter" onchange="submit()" valueChangeListener="#{workOrderMbean.onChangeMediaCombo}" immediate="true">
  <f:selectItems value="#{workOrderMbean.mediaCombo}"/>
</h:selectOneMenu>

<h:selectOneMenu id="hUnit" value="#{workOrderMbean.selectedHeightUnit}" converter="UnitConverter" >
  <f:selectItems value="#{workOrderMbean.unitCombo}"/>
</h:selectOneMenu>

onchane event of Mediacombo is

 public void onChangeMediaCombo(ValueChangeEvent e) throws SearchBLException {

        if (e.getNewValue() != null) {
            Media media = (Media) e.getNewValue();
            if (unitCombo != null && !unitCombo.isEmpty()) {
                unitCombo.clear();
                seclectedWidthUnit=new Unit();
               selectedHeightUnit=new Unit();
            }
            unitCombo = ComboLoader.getUnitsComboByMediaid(media.getMediaId());
        }
        else
        {
            if (unitCombo != null && !unitCombo.isEmpty()) {
                unitCombo.clear();
                seclectedWidthUnit=null;
                selectedHeightUnit=null;
            }
            unitCombo = ComboLoader.getUnitsComboByMediaid(-1);
        }
    }

i am also using converter for 'Unit'

my media converter is

@FacesConverter(value = "MediaConverter") public class MediaConverter implements Converter{

MediaDAO mediadao=new MediaDAOImpl();

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    Media media=null;
    try {
        media=mediadao.getMedia(Integer.parseInt(value));
    } catch (SearchBLException ex) {
        Logger.getLogger(MediaConverter.class.getName()).log(Level.SEVERE, null, ex);
    }
    return media;
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    String str = "";
if (value instanceof Media) {
    str = "" + ((Media) value).getMediaId();
}
return str;
}

}

1 Answers1

4

The problem is most likely in your converter and model class (Media).

You don't show how you exactly do the conversion, but I guess you're converting to String by returning the Media's Id, and converting back to Media by getting a new instance from some place like a DB?

In that case, your Media class needs to implement a custom equals and hashcode method.

JSF compares if the value send by the user corresponds with the values in the list you bind to the selectitems. It uses equals for that, which by default compares object Ids (kind of memory references). Unless you have the exact same instances, this will always be false.

Instead of defining an equals method, you can alternatively let your converter get the model object you need from the same list as the selectitems come from. There was an article on http://jdevelopment.nl a while back about this.

Mike Braun
  • 3,729
  • 17
  • 15
  • 1
    Hi Mike.. thanks a lot i got my problem. now i changed my converter as mentioned article. now its working fine.. Thank you thank you very much ... – Rahul Haldiya Feb 21 '12 at 09:31