jsf

How to disallow invalid LocalDate in f:convertDateTime?


I'm using JSF Mojarra 2.3.17 and JRE 21. My bean has a LocalDate variable and the following JSF page:

<h:inputText value="#{bean.date1flt}" pt:type="date">
  <f:convertDateTime type="localDate" pattern="yyyy-MM-dd" />
</h:inputText>

Why does it not have some sort of error message for "2024-06-31" when it's not a valid date (30 days in June). Instead, it sets the bean variable to null. If I try LocalDate.of(2024, 6, 31); I get java.time.DateTimeException: Invalid date 'JUNE 31', which is good, but nothing if I use JSF.


Solution

  • It should indeed have used non-lenient parsing. This is also specifically requested in the spec:

    In all cases, parsing must be non-lenient; the given string must strictly adhere to the parsing format.

    This worked for java.util.Date but not for its java.time.* cousins. That's thus a bug in Mojarra. I fixed it for Mojarra 4.0.10.

    In the meanwhile, your best bet is to use a custom converter.