If I have 2 forms inside a page, one to display a list of items
(selectOneMenu) and one to display de detail of the selected item whenever I
get a validation error the detail item form doesn't get filled with the data
of a new selection. Any idea why is this happening?

Thanks,

Johann

<%@ taglib uri="http://java.sun.com/jsf/html"; prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk"; prefix="t"%>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=UTF-8" />
<title>Sistema Ventas</title>
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath()
%>/css/common.css" />
</head>
<body>
<h2>Listado Ventas</h2>
<f:view>
  <t:messages id="messages" errorClass="error" infoClass="info"
replaceIdWithLabel="true" showSummary="false" showDetail="true" />
  <h:form id="headerForm">
    <h:panelGrid id="headerGrid" columns="2" columnClasses="header,detail"
styleClass="defaultTable">
      <t:outputLabel for="name" value="Nombre" />
      <t:selectOneMenu id="name" value="#{bean.item.name}"
valueChangeListener="#{bean.cargarItemEvent}" onchange="if(this.value!='-1')
submit();">
        <f:selectItem itemValue="-1" itemLabel="Nuevo"/>
        <f:selectItems value="#{bean.itemsAsMap}" />
      </t:selectOneMenu>
    </h:panelGrid>
  </h:form>
  <h:form id="detailForm">
    <h:panelGrid id="detailGrid" columns="2" columnClasses="header,detail"
styleClass="defaultTable">
      <t:outputLabel for="name" value="Nombre" />
      <t:inputText id="name" value="#{bean.item.name}" required="true" />
      <t:outputLabel for="description" value="Descripcion" />
      <t:inputTextarea id="description" value="#{bean.item.description}"
required="true" cols="50" rows="2" />
      <t:outputLabel for="status" value="Estado" />
      <t:selectOneMenu id="status" value="#{bean.item.status}">
        <f:selectItem itemLabel="#{bean.statuses[0]}" itemValue="0" />
        <f:selectItem itemLabel="#{bean.statuses[1]}" itemValue="1" />
        <f:selectItem itemLabel="#{bean.statuses[2]}" itemValue="2" />
      </t:selectOneMenu>
      <t:outputLabel for="price" value="Precio" />
      <t:inputText id="price" value="#{bean.item.price}" required="true" />
      <f:facet name="footer">
        <h:panelGroup>
          <h:commandButton action="#{bean.agregar}" value="Agregar" />
          <h:commandButton action="#{bean.actualizar}" value="Actualizar" />
          <h:commandButton action="#{bean.eliminar}" value="Eliminar" />
        </h:panelGroup>
      </f:facet>
    </h:panelGrid>
  </h:form>
</f:view>
</body>
</html>


package ventas;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;

public class Bean implements Serializable {
  private static Log log = LogFactory.getLog(Bean.class);

  private final static int DEFAULT_ROWS = 5;

  public final static int NO_ROW_SELECTED = -1;

  private String sortColumn = null;

  private boolean sortAscending = true;

  private int selectedRowIndex;

  private int rowIndex;

  private int rowsPerPage = DEFAULT_ROWS;

  private transient ItemsDao itemsDao;

  private Item item;

  private transient List items;

  public Bean() {
    setItems(getItems());
  }

  public Item getItem() {
    return item;
  }

  public void setItem(Item item) {
    this.item = item;
  }

  public String[] getStatuses() {
    return new String[] { "DISPONIBLE", "RESERVADO", "COMPRADO" };
  }

  //Sorting
  public String getSortColumn() {
    return sortColumn;
  }

  public void setSortColumn(String sortColumn) {
    this.sortColumn = sortColumn;
  }

  public boolean isSortAscending() {
    return sortAscending;
  }

  public void setSortAscending(boolean sortAscending) {
    this.sortAscending = sortAscending;
  }

  //Scrolling
  public Map getItemsAsMap() {
    List items = getItems();
    Map map = new HashMap();
    for (Iterator iter = items.iterator(); iter.hasNext();) {
      Item i = (Item) iter.next();
      map.put(i.getName(), i.getName());
    }
    return map;
  }

  public List getItems() {
    if (items == null) {
      List list = getItemsDao().readAllItems();
      if (list != null && list.size() > 0)
        items = list;
    }
    return items;
  }

  public void setItems(List records) {
    setRecords(items, DEFAULT_ROWS);
  }

  public void setRecords(List items, int rowsPerPage) {
    this.items = items;
    this.rowsPerPage = rowsPerPage;
  }

  public void setRowsPerPage(int rowsPerPage) {
    this.rowsPerPage = rowsPerPage;
  }

  public int getRowsPerPage() {
    return rowsPerPage;
  }

  public int getSelectedRowIndex() {
    return selectedRowIndex;
  }

  public void setSelectedRowIndex(int selectedRowIndex) {
    this.selectedRowIndex = selectedRowIndex;
    this.setRowIndex(selectedRowIndex);
  }

  public int getRowIndex() {
    log.debug("rowIndex: " + rowIndex);
    return rowIndex;
  }

  public void setRowIndex(int rowIndex) {
    log.debug("rowIndex1: " + rowIndex);
    this.rowIndex = roundDownRowIndex(rowIndex);
    log.debug("rowIndex2: " + this.rowIndex);
  }

  private int roundDownRowIndex(int rowIndex) {
    int page = rowIndex / rowsPerPage;
    int roundedIndex = page * rowsPerPage;
    return roundedIndex;
  }

  //Listeners
  public void cargarItemEvent(ValueChangeEvent event) {
    log.debug("cargarItem llamado para item [" + event.getNewValue() + "]");
    item.setName((String) event.getNewValue());
    cargarItem();
  }

  // Actions
  public String agregar() {
    log.debug("Agregar llamado para item [" + item + "]");
    getItemsDao().createItem(item);
    addInfo("Item [" + item.getName() + "] creado");
    //item = null;
    items = null;
    return null;
  }

  public String actualizar() {
    log.debug("actualizar llamado para item [" + item + "]");
    getItemsDao().updateItem(item);
    addInfo("Item [" + item.getName() + "] actualizado");
    //item = null;
    items = null;
    return null;
  }

  public String eliminar() {
    log.debug("eliminar llamado para item [" + item + "]");
    getItemsDao().deleteItem(item);
    addInfo("Item [" + item.getName() + "] eliminado");
    //item = null;
    items = null;
    return null;
  }

  public String cargarItem() {
    log.debug("cargarItem llamado para item [" + item + "]");
    item = getItemsDao().readItem(item.getName());
    return null;
  }

  //Privates
  private ItemsDao getItemsDao() {
    if (itemsDao == null) {
      ApplicationContext ac = (ApplicationContext)
FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(InitServlet.APP_CTX);
      itemsDao = (ItemsDao) ac.getBean("itemsDao");
    }
    return itemsDao;
  }

  private void addError(String msg) {
    FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage(FacesMessage.SEVERITY_ERROR, "", msg));
  }

  private void addInfo(String msg) {
    FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage(FacesMessage.SEVERITY_INFO, "", msg));
  }
}

-- 
View this message in context: 
http://www.nabble.com/MyFaces-Validation-problem-tf3635039.html#a10150314
Sent from the MyFaces - Users mailing list archive at Nabble.com.

Reply via email to