1

So I get a Validation Error: Value is not valid when submitting my form.

My enum looks like this:

public enum Version {
    none, v1_3("1.3"), v1_4("1.4");
        private BigDecimal version;

Version(String vers) {
    version = new BigDecimal(vers);
}

Version() {
}

public String toString() {
    if(version == null){
        return " ";
    } 
    else{
        return version.toPlainString();
    }
}
...

When I try to submit the following form I get the mentioned error:

<h:form>
                    <p>
                        <h:outputText value="Test Version: " />
                        <h:selectOneMenu binding="#{inputVersionTest}">
                            <f:selectItems value="#{myBean.getVersionValues()}" />
                        </h:selectOneMenu>
                    </p>
            <h:commandButton value="Test" type="submit" action="#{myBean.test(inputVersionTest.value)}" />
        </h:form> 

I played around with my toString() method in my Enum and when I return "none" (just like the enum value naming) if the version attribute is null, the JSF form is actually working. The problem is that the values in my dropdown menu do not mach the enum values.

The values for the drop down menu is created by this method in myBean:

public Version[] getVersionValues(){
        return Version.values();
    }

The toString() method converts those values to " ", 1.3 and 1.4. But what I would need to make it work (it seems) is none, v1_3 and v1_4. I also tried to create my own equals method in Version.java, but it didn't work. (equals(String) and equals(Version)).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kinglite
  • 339
  • 3
  • 20
  • You have to apply a converter for type "Version". Java enum values are classes, not integers. You have to specify how do you want to display them by the 'converter="myRegisteredCustomConverterID"' attribute or by a `` subtag. An example : https://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/ – The Bitman Apr 10 '17 at 07:21
  • Thank you, that works. If you can make your comment an answer, I can accept it : ) – kinglite Apr 10 '17 at 11:40

1 Answers1

0

If you have a value of a TObject descendant and you want to handle it in JSF pages then you have to define its apperarance by custom converters. (Java enum values are classes as well).

The custom converter:

@FacesConverter( "myCustomConverter" )
public class MyCustomConverter implements Converter
{
  @Override
  public Object getAsObject(FacesContext context, UIComponent component, String value)
  {

    //...
  }
  @Override
  public String getAsString(FacesContext context, UIComponent component, Object 
  value)
  {
    //...
 }
}

And its usage:

<h:form>
  <h:inputText value="#{myBean.myCustomValue}">
    <f:converter converterId="myCustomConverter">
  </h:inputText>
</h:form>

But JSF contains a builtin converter for enums. So you can use them just like the primitive types.

The example enum type:

public enum MyEnum
{
  VALUE1, VALUE2, VALUE3, VALUE4
}

The managed bean wich stores the enum value:

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@Named
@SessionScoped
public class MyManagedBean implements Serializable
{
    private MyEnum selectedMyEnum;

    public MyManagedBean()
    {}

    public MyEnum getSelectedMyEnum() { return selectedMyEnum; }
    public void setSelectedMyEnum( MyEnum selectedMyEnum_ ) { selectedMyEnum = selectedMyEnum_; }
    public MyEnum[] getMyEnumValues() { return MyEnum.values(); }
}

The facelets to demonstrate the MyEnum primitive type like usage:

index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core">
<h:body>
  <h:form>
            <h:selectOneRadio value="#{myManagedBean.selectedMyEnum}" layout="pageDirection">
                <f:selectItems value="#{myManagedBean.myEnumValues}"/>
            </h:selectOneRadio>
            <h:commandButton value="Submit" action="done"/>
    </h:form>
    </h:body>
</html>

done.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        The selected value is: #{myManagedBean.selectedMyEnum}
    </h:body>
</html>
The Bitman
  • 1,279
  • 1
  • 11
  • 25