Required field issue when javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL is set to TRUE

In both MyFaces and Mojarra 2.1 there is a defect where, when javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL set to true , any fields marked as required and pre-filled in the model when they are shaded and presented are shown with their original values ​​without attenuation, instead to stay empty.

Scenario:

  • The user loads a page with one required field filled with existing data from the model.
  • The user deletes the field on the page and submits the form
  • The validation failed, as expected, and the user is shown an error message that they must fill in the required field.

The problem is that the field in which what the user sent should be displayed, namely that they sent an empty value for the field, is instead filled with the original value from the model. The only workaround is to set javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to false . Setting it to false provides the desired behavior that the field remains drowned when the page is re-displayed with a mandatory field error message.

The defect was registered with Mojarra (http://java.net/jira/browse/JAVASERVERFACES-2375) and MyFaces (https://issues.apache.org/jira/browse/MYFACES-3525), but no progress was made through 6 weeks.

Please note that it seems that Mojarra had a similar problem, which was reported more than 6 mos ago has not yet been achieved.

Does anyone know of a workaround for this, where we can keep javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL true instead of false and still not run into this required field usability problem?

+6
source share
1 answer

This issue is explained in detail in JSF 2 - Bean Validation: Validation failed → empty values ​​are replaced with the last valid values ​​from the managed bean . By the time, in Mojarra, this is caused by an error, or at least the oversight in HtmlBasicRenderer#getCurrentValue() and reported as issue 2262 .

At the same time, the easiest way to work around this solution, given the libraries of third-party components with their own renderings, such as PrimeFaces, is to copy the source UIInput file directly to the project’s source folder and then edit getSubmittedValue() accordingly:

 public Object getSubmittedValue() { if (submittedValue == null && !isValid() && considerEmptyStringNull(FacesContext.getCurrentInstance())) { return ""; } else { return submittedValue; } } 

In the end, it will be /WEB-INF/classes , and this will take precedence when loading classes over the JSF JAR file. Admittedly, this is somewhat awkward, but less painful than rebuilding a JAR file or overriding each individual renderer.

+7
source

Source: https://habr.com/ru/post/916303/


All Articles