1
votes

Using JSF 2.0:

User loads a page with an input field that is bound to a backing bean field that has an existing value. The input field has been marked as required. If the user then blanks out the field and submits the form, the required message appears, as expected, however the value of the field is replaced with its original value when the page was rendered rather than leaving the field blank.

This behavior appears inconsistent with what happens when there is a format validation error on a field (vs a required validation error) e.g. if there is a field for a user to enter an email address on the page which is linked to an email format validator and the user replaces an existing valid email address that was populated in the field when the page was loaded with an invalid email address and submits the form, the validation error message appears and the current value of the email address field is left alone (and not replaced with its original value when the page was loaded).

Is there any way to leave the field as blank when a required field validation error occurs versus re-loading its original (non-blank) value?

More details:

  • Weblogic 10.3.2
  • MyFaces 2.0.12

JSF page:

<h:form>
  <h:messages showSummary="true"/>
  <h:inputText value="#{page1Controller.firstName}" 
               required="true" 
               requiredMessage="You must enter a first name"/>
  <h:commandButton value="Submit"/>
</h:form>

Managed Bean

@ManagedBean
public class Page1Controller
{

    public String getFirstName()
    {
        return "Default Value";
    }

    public void setFirstName(String value)
    {
        // no-op (for example purposes only)
    }

}
3
This is not the default behaviour. It should already work as you initially expected. Even more, I just tried a quick'n'dirty example with Mojarra 2.1.7 on Tomcat 7.0.26 and it just works for me. Please show an SSCCE and mention exact JSF impl/version and appserver impl/version. - BalusC

3 Answers

3
votes

Changing the context-param javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in web.xml from true to false fixed the issue.

0
votes

What's the scope of your backing bean?

The problem may be related to the scope, because the Bean could be reloaded after a Validation Error, causing the input field to show the original Bean's value

0
votes

I have the same problem (Glassfish with Mojarra 2.2.7)

If user edit a page with data already filled, then in one selectOneMenu he select empty option, when he submit he got the validation error (field empty) and the value is back.

To fix this I added this :

<h:selectOneMenu value="#{myBean.value}" validator="#{myBean.required}">
 <f:selectItem itemValue="#{null}" itemLabel="" />
 <f:selectItems value="#{myBean.list}" />
</h:selectOneMenu>

 public void required(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if(value == null) {
        ((UIInput) component).setValue(null);
        ((UIInput) component).setValid(false);
        throw new ValidatorException(new FacesMessage("Empty!"));
    }
}

But would be better if we could just use the "required=true". We don't have any settings for INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL so I don't think that's the reason?

[edit] Actually I notice it is only the case for input inside h:datatable