0

The following code:

<h:selectOneMenu id="discountCode" value="#{customerMBean.details.discountcode}"   
     title="DiscountCode" required="true" requiredMessage="The DiscountCode      field is required."  
     converter="#{customerMBean.discountCodeConverter}">   
     <f:selectItems value="#{customerMBean.allDiscountCodes}"/> 
</h:selectOneMenu>
<h:commandButton id="back" value="Back" action="#{customerMBean.list}"/>  
<h:commandButton id="update" value="Update" action="#{customerMBean.update}"/> 

Shows the following error, regardless of the clicked button:

j_idt6:discountCode : validation error .

public Converter getDiscountCodeConverter() {  
return discountCodeConverter;  
}

private Converter discountCodeConverter = new Converter() {  

@Override  
public Object getAsObject(FacesContext context, UIComponent component, String value) {  
    return new ConverterException("On verra la conversion String->Objet plus tard...");  
}  
    @Override  
   public String getAsString(FacesContext context, UIComponent component, Object value)     {  
    DiscountCode dc = (DiscountCode) value;  
    return dc.getDiscountcode()+" : "+dc.getRate()+"%";   
} 
};

How is this caused and how can I solve it?

marcAntoine
  • 668
  • 5
  • 19
  • 35

2 Answers2

2

When using a custom converter in h:selectOneMenu, you should always override the equals() method of the object involved.

See also

Community
  • 1
  • 1
Menno
  • 12,175
  • 14
  • 56
  • 88
  • forgive my ignorance because it's my first tutorial but what's the " equals() methods " – marcAntoine Oct 18 '12 at 08:27
  • The equals() method is not part of any JSF structure, it's part of plain old Java. This method is used to compare plain old POJO's. I have just found a link which may provide you with a solution to your problem: [link]http://www.crazysquirrel.com/computing/java/jsf/converter-validation-error.jspx[/link] – Menno Oct 18 '12 at 08:34
  • Have you read simular questions like [link]http://stackoverflow.com/questions/12207248/jsf-converter-issue-in-selectonemenu?rq=1[/link]? – Menno Oct 18 '12 at 20:42
0

Use <h:message/> tag it will show you validation error and update it on clicking of button or you can use auto update.

According to me :-

why your button is not working ?? because every time when you click on submit button it will show you validation error but you are not using that's why it is not visible on your screen.

And on back button use immediate=true it will skip the validation phase.

Hunter
  • 820
  • 8
  • 19
  • i tried the immediate=true , it shows me back the list but doesn't update the values i entered ( the page CustomerDetails.xhtml contains a from : name email phone etc ..) when i click on button update after changing one value it's supposed to save it and update the list – marcAntoine Oct 18 '12 at 07:52
  • so after clicking on button update the selectonemenu. if this will not work then try this it will definitely work -> In your managed bean in update function clear list and recreate again and update the select one menu – Hunter Oct 18 '12 at 07:59