0
votes

I am having trouble to find my fault when recieving the Error "Validation Error: Value is not valid". After submitting a form, im getting this error, even though, when debugging, the selected Component is the right one. The converter seems to be working fine too thats why I am having a really tough time here. Here is what I have tried so far:

  • Change @RequestScoped to @ManagedBean and @ViewScoped - Made the SelectOneMenue beofore crash everytime because of a NullpointerException, this did never happen with @RequestScoped, so I am not sure what is happening there...

  • I double checked the Converter for this SelectedOneMenue, it seems to be working, I made a lot of debugging there, the Object that was converted to a String is, at least visible for me, the same that is later converted back from String to the Object.

  • The equals() Method: The equals Method seems OK to me, it literally just checks if the .id is the same, when checking this at the converter, it is always right. Maybe I'd rather search here for the error, even though I cant figure out what could be possibly wrong here.

Here are the parts of my code that I guess will be interesting for you, I have already made quite some research, I found quite good stuff though I couldn't get this to work but this must not mean anything since I just started programming with JavaEE and JSF ( + Primefaces).

Component equals() Method

 @Override
 public boolean equals(Object obj) {
    if(obj == null){
        //System.out.println("Component is NULL in equals() method");
    }
    if(this.id == ((Component) obj).id) {
        return true;
    }else {
        return false;
    }
}

Component Converter Class

@ManagedBean(name = "componentConverterBean")
@FacesConverter (value = "componentConverter") 
public class ComponentConverter implements Converter{

@PersistenceContext(unitName="PU")
private EntityManager em;

@Override
public String getAsString(FacesContext context, UIComponent component, Object modelValue) {
    if (modelValue == null) {
        return "";
    }
    return modelValue.toString();
}

@Override
public Object getAsObject(FacesContext context, UIComponent component, String submittedValue) {
    if (submittedValue == null || submittedValue.isEmpty()) {
        System.out.println("ComponentConverter: submittedValue is null");
        return null;
    }

    try {
        System.out.println("ComponentConverter: Value to be found: " + submittedValue);
        return em.find(Component.class, Integer.parseInt(submittedValue));
    } catch (NumberFormatException e) {
        throw new ConverterException(new FacesMessage(submittedValue + " is not a valid Component ID", e.getMessage()));
    }
}

}

Part of the .xhtml file containing the SelectOneMenues

 <p:outputLabel for="facility" value="Facility: " />
 <p:selectOneMenu id="facility" value="#{visualization.facility}" converter="#{facilityConverterBean}" style="width:150px">
    <p:ajax process="@this" partialSubmit="true" listener="#{visualization.onFacilityChange()}" update="component" />
    <f:selectItem itemLabel="Select Facility" noSelectionOption="true" />
    <f:selectItems value="#{visualization.facilities}" var="facility" itemLabel="#{facility.name}" itemValue="#{facility}" />
 </p:selectOneMenu>
 <p:outputLabel for="component" value="Components: " />
 <p:selectOneMenu id="component" value="#{visualization.component}" converter="#{componentConverterBean}" style="width:150px">
    <f:selectItem itemLabel="Select Component" noSelectionOption="true" />
    <f:selectItems value="#{visualization.components}" var="comp" itemLabel="#{comp.name}" itemValue="#{comp}" />
 </p:selectOneMenu>

"Components: Überprüfungsfehler: Wert ist ungültig." This is the exact Error, which translates to: "Components: Validation error: Value is not valid. "

I am very grateful for any help here. Thanks in advance.

1
so the facility related selectOneMenu works? And how and when do you submit the 'component' related one, since only then the selected 'component' is send to the serverKukeltje
The facility one seems to work, its pretty much the same Converter/Equals Method too, facility does not give me an error. After selecting everything the user will press Submit and then depending on chosen facility, component and from/to date i want to collect other data from the database. Does that answer your question?Max R.
@MaxR. Does modelValue.toString() returns Id of modelValue as String ?శ్రీ
Yes, i debugged it, the toString() method gives the right id back every time.Max R.

1 Answers

0
votes

Use a container object instead of an entity for value="#{visualization.component}". e.g.

class ComponentDto {
     private Component componentEntity;

     private Component getComponentEntity() {
         return this.componentEntity;
     }

     private void setComponentEntity(Component componentEntity) {
         this.componentEntity = componentEntity;
     }
}

See also: https://github.com/primefaces/primefaces/issues/2756