12


I do not understand the behaviour of JSF2 during valdation. Hope someone can help me.

I have a form where the fields are validated after (ajax) submit - ok
If the validation failed a error message is shown - ok

For my example when I enter a valid birthday and the field name is empty an errormessage for name is shown after submit.
Now when I enter a valid name and delete the input from the birthday field an errormessage is show for birthday (that's ok) but now the old 'valid' birthday stands also in the input field!?!

How can I avoid this behaviour? When I submit an empty field I want to see an errormessage and an empty field...

Here's my sample code:

I use a ManagedBean (TestBean) that contains an EntityBean (Contact). The Contact contains validations per annoations.

public class Contact implements Serializable {
    @NotNull 
    @Temporal(TemporalType.DATE)
    private Date birthday;

    @NotNull 
    @Size(min=3, max=15)
    private String name;

    //...
}

My ManagedBean:

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {
    private Contact contact;

    @PostConstruct
    void init() {
        System.out.println("init...");
        contact = new Contact(); 
    }

    public void newContact(ActionEvent ae) {
        System.out.println("newContact...");
        contact = new Contact();
    }

    public void save() {
        System.out.println("save...");
        //TODO do something with contact...
    }

    public Contact getContact() { return contact; }

    public void setContact(Contact contact) {this.contact = contact;}
}

An here my JSF page:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" >      
 <h:body>
    <h:form>     
        <h:panelGrid columns="3">   

        <h:outputText value="Birthday: " />  
        <h:inputText id="birthday" value="#{testBean.contact.birthday}">
            <f:convertDateTime/>
        </h:inputText>  
        <h:message for="birthday" />  

        <h:outputText value="Name: " />  
        <h:inputText id="name" value="#{testBean.contact.name}"/>
        <h:message for="name" />

        </h:panelGrid>  

        <h:commandButton value="submit" action="#{testBean.save}"> 
            <f:ajax execute="@form" render="@form"/>
        </h:commandButton> 

        <h:commandButton value="newContact" actionListener="#{testBean.newContact}"
                         immediate="true"> 
            <f:ajax render="@form"/>
        </h:commandButton> 

    </h:form>
</h:body>
</html>

at last a snippet from web.xml

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
    <param-value>true</param-value>
</context-param>

Thanks for some tips

Matthias Reining
  • 143
  • 1
  • 1
  • 6

4 Answers4

10

Your particular problem is caused by

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>

and a bug (at least, an oversight) in HtmlBasicRenderer#getCurrentValue() of Mojarra:

if (component instanceof UIInput) {
    Object submittedValue = ((UIInput) component).getSubmittedValue();
    if (submittedValue != null) {
        // value may not be a String...
        return submittedValue.toString();
    }
}

String currentValue = null;
Object currentObj = getValue(component);
if (currentObj != null) {
    currentValue = getFormattedValue(context, component, currentObj);
}
return currentValue;

Normally, the submitted value is set to null when the UIInput component is successfully converted and validated. When JSF is about to redisplay the value, it first checks if the submitted value is not null before proceeding to redisplay the model value. However, with this context parameter, it is null instead of an empty string when it is invalid and thus it will always redisplay the original model value when you remove the initial value of a required field.

To test it, set that context param value to false or remove it altogether. You'll see that it works as intended. However, it will bring back the disadvantage that your model values will be cluttered with empty strings on empty but non-required fields and you'll lose the advantage of using @NotNull annotation of JSR 303 bean validation.

To fix this, you've to alter the first part of HtmlBasicRenderer#getCurrentValue() as follows:

if (component instanceof UIInput && !((UIInput) component).isValid()) {
    Object submittedValue = ((UIInput) component).getSubmittedValue();
    if (submittedValue != null) {
        // value may not be a String...
        return submittedValue.toString();
    } else {
        return null;
    }
}

I've already reported it to Mojarra guys as issue 2266.

Martin Höller
  • 2,714
  • 26
  • 44
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I have noticed similar behavior when an input is bound to a non-String property with a converter, possibly due to the same root cause. When you put in an empty string and the validation fails because of a different field, your (valid) empty string is overwritten with the value from the bean. Was reported as http://java.net/jira/browse/JAVASERVERFACES-838 – wrschneider Dec 07 '11 at 02:11
  • 2
    @BalusC: Thank you for your detailed explanation! Your solutions/explanations here often make my day ;-) – Matthias Reining Dec 30 '11 at 14:09
  • @BalusC Does that mean that he compile a custom version of JSF? I have run into this same issue and hoping that this isn't the only solution. – MikeR May 16 '13 at 15:14
  • @Mike: For starters, easiest way is to copypaste raw `.java` source file of `HtmlBasicRenderer` straight into your webapp project (while keeping the original package!) and then edit it. It'll get precedence in classloading over the JAR in `/WEB-INF/lib`. – BalusC May 16 '13 at 15:20
0

Had a similar issue where a value loaded in from the backing bean would get reset when the field was blanked out and another component failed validation. I had to make a slight addition to BalusC's code to make it work.

protected String getCurrentValue(FacesContext context,
                                     UIComponent component) {

        if (component instanceof UIInput && !((UIInput) component).isValid()) {
            Object submittedValue = ((UIInput) component).getSubmittedValue();
            if (submittedValue != null) {
                // value may not be a String...
                return submittedValue.toString();
            } else {
                return null;
            }
        }


        String currentValue = null;
        Object currentObj;

        if ( component instanceof UIInput && ((UIInput)component).isLocalValueSet() )
        {
           currentObj = ((UIInput)component).getLocalValue();
        }
        else {
            currentObj = getValue(component);
        }

        if (currentObj != null) {
            currentValue = getFormattedValue(context, component, currentObj);
        }
        return currentValue;


    }
andyfinch
  • 1,312
  • 2
  • 19
  • 34
0

I'm thinking that during normal use people won't enter a valid date, submit, and then delete the date before submitting again. I realize that you found this during testing, but probably people are just trying to successfully fill out the form and not deleting stuff they already entered, in which case perhaps keeping the last valid value is the best functionality.

If you insist... It seems like the setter method "birthday" is never called because the value is invalid, and then when the page is redisplayed the current value of "birthday" is displayed (the current value being the valid value that was previously saved). Maybe you could write a custom validator that sets the value and THEN validates the value, but this wouldn't really make much sense. You would still have to validate the value first for cases when the users enters a text string like "yesterday" instead of a valid date, and then you would have to set the date to something based on that invalid value, and then you would have to add the message to the FacesContext. So the code would have to do something like the following.

1) validate the date value
2) if it's invalid then set the field to some value which makes sense and add an error message to FacesContext.
3) if it's valid then use it.

That's do-able, but weird because you're changing the value of the field even though the passed in value is invalid...

AaronJ
  • 1,060
  • 10
  • 24
  • Hi Aaron, thanks for your answert! I my example code there's another case which show the same problem while trying to replace the backend model (button newContact). For me it looks like a real world problem - and not like an academic test case. I found a solution (not nice, but it works...). For the solutions I have edited the sample code a little bit. See my own answer – Matthias Reining Oct 26 '10 at 22:29
0

Aaron already descripes the behaviour.

The problem I've been described exists also by clicking the 'newContact' button. If the first submit is not valid (birthday was entered, name-field is empty) an error message is shown. ok.

Afterwards the 'newContact' Button do not refresh (clear) the view. Although the model was resetted (contact = new Contact()).

I found some tipps here: http://wiki.apache.org/myfaces/ClearInputComponents

Here is my solution:

public void newContact(ActionEvent ae) {
    contact = new Contact();
    contact.setBirthday(new Date()); //for testing only

    resetForm(ae.getComponent());
}

private void resetForm(UIComponent uiComponent) {
    //get form component
    UIComponent parentComponent = uiComponent.getParent();
    if (uiComponent instanceof UIForm)
        resetFields(uiComponent);
    else if (parentComponent != null)
        resetForm(parentComponent);
    else
        resetFields(uiComponent);

}

private void resetFields(UIComponent baseComponent) {
    for (UIComponent c : baseComponent.getChildren()) {
        if (c.getChildCount() > 0)
            resetFields(c);

        if (c instanceof UIInput)
            ((UIInput) c).resetValue();
    }
}
Matthias Reining
  • 143
  • 1
  • 1
  • 6