0

I am trying to enter input in a datatable (sample code below). The really difficult part of solving this problem is that I can't reproduce it. It works for a while with valid error messages and I am able to reenter data. Then it gets stuck on some bad data and ignores subsequent input. I know this is a question that has been asked before. I have tried all the solutions in How can I populate a text field using PrimeFaces AJAX after validation errors occur? (which is 8 years old) and other posts.

One problem with some of the "reset" solutions is after a validation error, data gets reset to the original model data and there is no indication of an error and the old data is simply resubmitted.

Here is very simplified version of my xhtml and my validator. Again, when I run this it will work. (My actual xhtml is obviously more complex). Then after multiple entries of good and bad data, it gets stuck on some bad data and stops updating. Perhaps the fact that the data are inside a datatable is an issue. If anybody has solutions please let me know. Even a workaround in my reset button would be ok at this point.

<h:form id="swFormId">

<h:panelGrid columns="3">
    <h:outputText value="Simple"/>
    <p:commandButton id="submitId1" value="Submit" action="#{controller.submitAction}" update="@form">
        <!--<f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener"/>-->
    </p:commandButton>

    <p:commandButton value="Refresh" action="#{controller.refreshAction}" resetValues="true" update="@form"/>
</h:panelGrid>

<p:dataTable value="#{controller.swContainers}" var="swContainer" rows="5">
    <p:column headerText="Container">
        <p:outputLabel value="#{swContainer.containerNumber}"/>
    </p:column>

    <p:column headerText="Value">
        <p:inputText id="inputId" value="#{swContainer.currentSwLabel.phString}">
            <f:attribute name="aContainer" value="#{swContainer}"/>
            <f:validator validatorId="simpleValidator"/>
        </p:inputText>
        <h:message for="inputId" style="color:red"/>
    </p:column>
</p:dataTable>

Converter:

 @FacesValidator("simpleValidator")
 public class SimpleValidator implements Validator {

private static final Logger logger =    LoggerFactory.getLogger(SimpleValidator.class);

public SimpleValidator() {
}

@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    SwContainer aContainer = (SwContainer) component.getAttributes().get("aContainer");
    String message = null;
    Double phFloat = null;
    if (StringUtils.isEmpty(value+"")) {
        message = "value is required " ;
    } else {
        try {
            phFloat = Double.valueOf(value.toString());
        } catch (NumberFormatException e) {
            message = "phValue must be a number";

    } catch (Exception e) {
        message = e.toString() ;
    }
}
    logger.info("~ " + aContainer.getContainerNumber() + " value {" + value + "} message {" + message + "}");
    if (message == null) {
        logger.debug("value ok " + value +" " + aContainer.getContainerNumber());
        aContainer.getCurrentSwLabel().setPhValue(phFloat);
    } else {
        logger.debug("throwing ValidatorException for " + aContainer.getContainerNumber() + " message: " + message);
        throw new ValidatorException(new FacesMessage(message));
    }
  }
}

I've also tried adding the following from omnifaces to my faces-config.xml

  <application>
    <action-listener>org.omnifaces.eventlistener.ResetInputAjaxActionListener</action-listener>
</application>

<

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
user6627139
  • 406
  • 6
  • 16

0 Answers0