开发者

UIInput values inside cc:insertChildren are not redisplayed after validation failure

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 v开发者_如何学编程alue 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.

UIInput values inside cc:insertChildren are not redisplayed after validation failure


There's some confusion going on. The value is only reverted back to the initial value when the submitted value is empty (i.e. the user removed the initial value or just entered nothing). I'm not sure how the problem of having to retype the whole text is related to this as actually nothing has been entered.

When the submitted value is empty or null, JSF will just redisplay the model value, else it will redisplay the submitted value, exactly as you'd expect. This is specified in JSF specification. To see it yourself, add for example

<f:validateRegex pattern="([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)" />

to the email input field. You'll see that any non-empty(!) submitted value is retained in case of invalid email formats.


Update: as per your update it turns out that your input components are actually been rendered by <cc:insertChildren>. This behaviour is indeed a bug in Mojarra. See issue 1991. As mentioned in the issue ticket, the workaround is to put the <cc:insertChildren /> inside a <h:panelGroup> or an <ui:fragment>.

<cc:implementation>
    <ui:fragment>        
       <cc:insertChildren/>        
    </ui:fragment>
</cc:implementation>

MyFaces 2.1.1 doesn't have this bug and it works fine over there.


Maybe your have some code which do clear submitted values of input components inside that panelGroup (for example the panelGroup has specific id, or that code do more generic analysis looking up for panelGroups inside forms).

Try doing full-text search through your project source code for setSubmittedValue calls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜