Validation messages displayed and cleared immediately

405 Views Asked by At

I am using JSF 2.1.21 and my error messages are displayed and then cleared immediately when I press enter on the input text box. I have a javascript on the input which triggers a commandbutton.

<h:form>
<div><ice:messages /></div>
<ice:inputText  onkeyup="submitOnEnter(event);" value="#{bean.name}" maxlength="6" size="6">
                                <f:validator validatorId="stringValidator" />
</ice:inputText>
<ice:panelGroup>
   <ice:commandButton id="button" value="Go"    actionListener="#{myController.getReport}">
   </ice:commandButton>
</ice:panelGroup>
</h:form>

stringValidator:

public void validate(FacesContext context, UIComponent component, Object value) throws 
    ValidatorException {
if (!(((String) value).trim()).matches(REGEX_CONSTANT)) {
        ((UIInput) component).setValid(false);
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, 
                                        myMessage, null));
    } else {
        ((UIInput) component).setValid(true);
    }

Update:

function submitOnEnter(event) {
   var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
   if(keyCode == 13) 
   {
           document.getElementById("myForm:button").click();
           return false;
   }
}
0

There are 0 best solutions below