Update: Updated once more now. I think my previous analysis was wrong, because I have now been able to create an example for this. It appears to be related to composite components and the insertChildren tag.
Here is my facelet:
<?xml version='1.0' encoding='UTF-8' ?>
<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:form="http://java.sun.com/jsf/composite/composite/form"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head></h:head>
<h:body>
<h:form>
<form:workspace>
<h:inputText id="email" value="#{user.email}" size="45"
required="true" requiredMessage="Required">
<f:validateRegex pattern="([^.@]+)(\.[^.@]+)*@([^.@]+\.)+([^.@]+)" />
</h:inputText>
<h:message for="email" />
<br />
<h:commandButton action="#{user.update}" value="Update"/>
</form:workspace>
</h:form>
</h:body>
</html>
The form:workspace composite component is defined as follows:
<?xml version='1.0' encoding='UTF-8' ?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
</cc:interface>
<cc:implementation>
<cc:insertChildren/>
</cc:implementation>
</html>
The #{user}
variable refers to a trivial bean with just a field called email and a getter and a setter. The update method does nothing.
The behavior I get is as described below. When I enter an invalid value, it is reverted back to what it is stored in the bean. If I remove the wrapping by the form:workspace component, it works like I expect - the value is not reverted. JSF bug?
I have a JSF form with a number of input components. Some of these have validators attached to them. For example, a simple one is the e-mail address, which is mandatory:
<h:inputText id="email" value="#{profile.user.email}" size="45"
required="true" requiredMessage="Required">
Now, we have received a requirement that fields that are invalid should keep the submitted value in them, so the user does not have retype a lot of text for simple spelling mistakes. This seems very reasonable to me, and I even expected this to be the default behavior of validation. But it doesn't seem to be - the value is reverted back to what it was before (when it was still valid).
The problem is, I cannot even find a reference for what the behavior should be. I read somewhere that it's the local value of the UIInput that is displayed after validation. Inspecting the code of UIInput tells me that local value is intentionally not set if validation fails. But this does not seem to be the whole truth, because ALL input fields are reverted - not just the ones for which validation failed! So it looks more like it gets the values from the model again.
In any event, is there a way I circumvent this behavior?
I am using JSF 2.0.4-b09. I feel like I am missing something obvious.
Edit: I changed the e-mail validator to the regexp one BalusC posted below, and I still get the same behavior. I posted an image illustrating the problem. The upper part of the image shows the initial state of the form. Then the middle part shows that I change the fields "First name", "Last name" and "Email". The bottom part shows the result after I click the save button. As you can see, the e-mail field is reverted. Not only that, the first name and last name fields are also reverted.