1

When the date format is not correct (for example when I manually post 13,02,2018 instead of 13.02.2018 and also other incorrect dates such as 13.02.999) the app crashes. How can I fix it? (the manual input is important, i can`t just disable it).

XHTML:

<rich:calendar enableManualInput="true" datePattern="dd.MM.yyyy"
    value="#{myBean.data.myDate}">
    <f:converter converterId="mydate"/>
</rich:calendar>

Converter:

@FacesConverter("mydate")
public class LocalDateConverter implements Converter {
    private static final DateTimeFormatter formatter;
    static {
        formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
        formatter.withLocale(new Locale("ru"));
    }
    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return LocalDate.parse(value, formatter); 
    }

    @Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
    if (value == null) {
        return "";
    } else if (value instanceof LocalDate) {
        return ((LocalDate) value).format(formatter);
    } else if (value instanceof LocalDateTime) {
        return ((LocalDateTime) value).format(formatter);
    } else {
        throw new IllegalArgumentException("Value is not java.time.LocaleDate");
    }
}
Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
Yiour
  • 177
  • 1
  • 5
  • 15

3 Answers3

2

Converters should throw a ConverterException which can contain a FacesMessage. This message can be displayed on your XHTML page, near the input component that caused the exception using <h:message for="inputComponentId"/>.

The problem occurs in your getAsObject method. There you should catch the DateTimeParseException exception and throw a ConverterException:

try {
  return LocalDate.parse(value, formatter);
}
catch (DateTimeParseException ex) {
  throw new ConverterException(new FacesMessage("Invalid date: " + value));
}

See also:

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
0

You don't need converter at all. Simply include label attribute in rich:calendar component and let system figure out if value is correct. Example:

<h:outputLabel for="programStartDate" value="#{msg.programStartDate}" />
<rich:calendar id="programStartDate" value="#{program.programStartDate}"
    label="#{msg.programStartDate}" inputStyle="width: 100px;"
    datePattern="#{referenceData.defaultDatePattern}"
    timeZone="#{referenceData.timezone}"
    enableManualInput="true" popup="true" required="true" />
Vasil Lukach
  • 3,658
  • 3
  • 31
  • 40
  • I don't know RichFaces that well, and could not find it in the documentation, but does `rich:calendar` support conversion to `LocalDateTime` that the OP seems to be using? – Jasper de Vries Feb 14 '18 at 21:13
  • @Jasper de Vries there are `datePattern` and `timeZone` attributes for that purpose – Vasil Lukach Feb 14 '18 at 21:17
-2

use a try catch and catch the exception so it doesn't crash but continue without allowing the exception to crash your program

@FacesConverter("mydate")
public class LocalDateConverter implements Converter
{
    private static final DateTimeFormatter formatter;
    static {
        formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy");
        formatter.withLocale(new Locale("ru"));
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value)
    {
        return LocalDate.parse(value, formatter);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value)
    {
        try
        {
            if (value == null)
            {
                return "";
            }
            else if (value instanceof LocalDate) {
                return ((LocalDate)value).format(formatter);
            } else if (value instanceof LocalDateTime) {
                return ((LocalDateTime)value).format(formatter);
            } else {
                throw new IllegalArgumentException("Value is not java.time.LocaleDate");
            }
        }
        catch (Exception)
        {
            return "SOME DEFAULT DATE";
        }

    }
}
mhoe4
  • 15
  • 3
  • 2
    I would expect the problem to be in the getAsObject and not the getAsString since the former is called when the client in the browser enters a string as a date – Kukeltje Feb 13 '18 at 18:10
  • 2
    It will be totally unhelpful to the end user to replace it's input with some default date. Just show an error message and allow the user to fix the input. – Jasper de Vries Feb 14 '18 at 21:38