PrimeFaces calendar accepts invalid dates as input

后端 未结 2 1325
再見小時候
再見小時候 2020-12-09 12:55

The problem I am having is with the PrimesFaces 3.4.1 calendar. When using the popup date picker activated either through the button or on input field focus you can only sel

2条回答
  •  时光取名叫无心
    2020-12-09 13:40

    In faces-config.xml add this

    
        localDateConverter
        com.utility.LocalDateConverter
    
    

    In the above class i.e LocaldateConverter add this below code

    /**
     * @param facesContext .
     * @param uiComponent .
     * @param input .
     * @return Object .
     */
    @Override
    public Object getAsObject(final FacesContext facesContext, final UIComponent   uiComponent, final String input) {
        if (StringUtils.isBlank(input)) {
            return null;
        }
        final String componentPattern = (String) uiComponent.getAttributes().get("datePattern");
        final String patternToUse = componentPattern != null ? componentPattern : CommonConstants.OUTPUT_DATE_FORMAT;
        try {
            final DateFormat fmt = new SimpleDateFormat(patternToUse);
            Date convertedDate = new java.sql.Date(fmt.parse(input).getTime());
            return convertedDate;
        } catch (Exception e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Invalid Date Format", null));
        }
    }
    
    /**
     * @param facesContext .
     * @param uiComponent .
     * @param obj .
     * @return String .
     */
    @Override
    public String getAsString(final FacesContext facesContext, final UIComponent uiComponent, final Object obj) {
        if (obj==null) {
            return null;
        }
        final Date date = (Date) obj;
        return date.toString();
    }
    

提交回复
热议问题