Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validation error value is not valid [duplicate]

I know this is a common error, and that there is a lots of questions about it, but I must create new one.

Here is my jsf page:

<h:form id="clientCreatingForm">
    <p:selectOneMenu id="city" value="#{kkmBean.client.address.city}" style="width: 263px;" 
          converter="cityConveter" required="true" requiredMessage="You must choose one city">
          <f:selectItems value="#{kkmBean.cityList}" var="city" itemLabel="#{city.cityName}" itemValue="#{city.cityID}"/>
     </p:selectOneMenu>


     <p:commandButton value="Save client" type="button" onclick="confirm.show()" style="margin-top: 20px; margin-left: 30px; width: 180px;" />
     <p:commandButton value="Reset fields" type="reset" style="margin-top: 20px; margin-left: 30px; width: 180px;"/>

          <p:confirmDialog message="Are you sure, you want to save client?" 
               header="Save client" severity="info" widgetVar="confirm">

               <p:commandButton value="Yes, I am sure." oncomplete="confirm.hide()" 
                                         action="#{kkmBean.saveClient}" update=":warnings" style="font-size: 12px;"/>
               <p:commandButton value="No, I don't!" onclick="confirm.hide()" type="button" style="font-size: 12px;"/>
          </p:confirmDialog>
</h:form>

Here is managed bean:

@ManagedBean(name = "kkmBean")
@SessionScoped
public class CreatingClientManager implements Serializable {

/** Creates a new instance of CreatingClientManager */
public KreiranjeKorisnikaManager() {
    client= new Client();
    client.setAddress(new Address());
    cityList = new LinkedList<City>();
    cityList = Communication.getInstance().getCitySessionBeanRemote().getCities();
  }

private Client client;
private List<City> cityList;

public String startCreatingClent() {

    return "creating-client";
  }

/**
 * @return the client
 */
public Klijent getClient() {
    return client;
  }

/**
 * @param client the client to set
 */
public void setClient(Client client) {
    this.Client = client;
  }

/**
 * @return the cityList
 */
public List<Grad> getCityList() {
    return cityList;
  }

/**
 * @param cityList the cityList to set
 */
public void setCityList(List<City> cityList) {
    this.cityList = cityList;
  }

public String saveClient() {
    System.out.println("i am in saveClient method");
    String message = Communication.getInstance().getClientSessionBeanRemote.saveClient(client);
    FacesContext context = FacesContext.getCurrentInstance();
    kontekst.addMessage(message, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success: ", message));
    return null;
  }
}

And finally converter:

   @FacesConverter(forClass=City.class, value="cityConverter")
   public class CityConverter implements Converter {

   @Override
   public Object getAsObject(FacesContext context, UIComponent component, String value) {
      Integer cityID = Integer.valueOf(value);
      CreatingClientManager kkm = (CreatingClientManager) ((HttpSession)context.getExternalContext().getSession(true)).getAttribute("kkmBean");
      List<Grad> cityList = kkm.getCityList();
      for (int i = 0; i < cityList.size(); i++) {
          City city = cityList.get(i);
          if(city.getCityID().equals(cityID)) {
              System.out.println(city.getCityName());
              return city;
          }
      }
      return null;
  }

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

Of course the problem is in selectOneMenu. When I submit form, i got this annoying error.

I was debugging converter and the line before return grad, i print grad.getImeGrada() and everything is fine, the name (Ime) of found city (grad) is printed. Am I missing something?

p.s. Sorry on my English, I hope that you understand what is my problem.

Almost forgot to mention that i override equals and hashCode methods, and i am sure it is ok.

like image 614
CallanSM Avatar asked Mar 23 '26 09:03

CallanSM


1 Answers

The way how you specified the itemValue of <f:selectItems> and implemented the getAsString() of the converter is not right.

When the selected value is submitted, JSF will convert the cityID to City by getAsObject() of the converter. Then, JSF will validate the City against the itemValue of the <f:selectItems> to see if it is really part of the list (this is to avoid hackers which modifies/tampers requests with a different value). However, since itemValue is cityID which is an Integer the comparison fails for every item of the list. Hence the Value not valid error.

You need to change itemValue to be City instead and change getAsString() to return the cityID:

<p:selectOneMenu id="city" value="#{kkmBean.client.address.city}" style="width: 263px;" 
      converter="cityConveter" required="true" requiredMessage="You must choose one city">
      <f:selectItems value="#{kkmBean.cityList}" var="city" itemLabel="#{city.cityName}" itemValue="#{city}"/>
 </p:selectOneMenu>

and

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    City city = (City) value;
    return city.getCityID() != null ? String.valueOf(city.getCityID()) : null;
}

Summarized: the type of the itemValue must be exactly the same as the type of the dropdown value and the converter must convert between exactly that type and string.

See also:

  • Validation Error: Value is not valid
like image 177
BalusC Avatar answered Mar 24 '26 22:03

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!