Search code examples
validationjsfprimefacesajax-update

Primefaces JSF update after validation failed doesn't work


I have a problem with the validation of a <p:inputText> and updating its content.

Basically when the inputText validation fails, it never gets updated again.

Here's a simple example to clarify:

The Facelet:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <body>
    <h1>Test</h1>

        <h:form id="list" prependId="false">
            <ul>
                <li>Element 1&#160;
                    <p:commandLink action="#{Test.assignElement}" update="detail_value">
                        <f:setPropertyActionListener target="#{Test.currentElement}" value="1" />
                        Assign
                    </p:commandLink>
                </li>
                <li>Element 2&#160;
                    <p:commandLink action="#{Test.assignElement}" update="detail_value">
                        <f:setPropertyActionListener target="#{Test.currentElement}" value="2" />
                        Assign
                    </p:commandLink>
                </li>
            </ul>
        </h:form>

        <h:form id="detail" prependId="false">
            <p:inputText value="#{Test.element}" id="detail_value" required="true" styleClass="#{Faces.messagesFor['detail_value'] ? 'border:1px solid red' : ''}">
                <p:ajax event="blur" update="detail_value"></p:ajax>
            </p:inputText>
        </h:form>
    </body>
</html>

The Test bean:

package com.easydevel.test;

public class Test {

    private String currentElement;
    private String element;

    public String getCurrentElement() {
        return currentElement;
    }

    public void setCurrentElement(String currentElement) {
        this.currentElement = currentElement;
    }

    public String getElement() {
        return element;
    }

    public void setElement(String element) {
        this.element = element;
    }

    public String assignElement(){
        setElement(getCurrentElement());
        return "";
    }

}

If you click on the commandLinks below the "Element"s the input field gets updated, but when a validation fails (simply leave the input text blank, and click on any other part of the page), the border of the input turns red. After that it never gets updated again when clicking on the above mentioned commandLinks.

Any ideas?


Solution

  • Arjan Tijms' answer will work, however the best solutions I found are:

    1. Use OmniFaces Solution so, instead of implementing the listener yourself all you need is just one line of code.
    <h:commandButton value="Update" action="#{bean.updateOtherInputs}">
      <f:ajax execute="currentInputs" render="otherInputs" />
      <f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener" />
    </h:commandButton>
    
    1. If you are using PrimeFaces you can use resetInput tag handler:
    <p:commandButton value="Reset p:ajax">
       <p:ajax update="panel" resetValues="true"/>
    </p:commandButton>