我的XHTML中包含以下内容
<p:calendar
id="endDate"
styleClass="dateWidth"
mask="99/99/9999"
value="#{inputController.endDate}"
showOn="button"
pattern="MM/dd/yyyy" >
<f:validator validatorId="net.elements.ui.DateValidator" />
</p:calendar>
以下是我的DateValidator
package net.elements.ui;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import net.stafftrack.ui.web.jsf.controller.StaffTrackJSFController;
@FacesValidator(DateValidator.VALIDATOR_ID)
public class DateValidator implements Validator
{
public static final String VALIDATOR_ID = "net.elements.ui.DateValidator";
@Override
public void validate(FacesContext facesContext, UIComponent component, Object value) throws ValidatorException
{
try
{
Date inputDate = (Date) value;
if (inputDate == null)
{
//Validation message
return;
}
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
sdf.setLenient(false);
sdf.parse(sdf.format(inputDate));
}
catch (ParseException e)
{
//Validation message
return;
}
catch (IllegalArgumentException e)
{
//Validation message
return;
}
}
}
当值为空时,这将被正确调用,但是如果我输入类似99/99/9999
的值,则不会被调用
我该怎么做?
我正在使用Primefaces 6.2
有人可以帮我吗?