Created
April 26, 2015 14:04
-
-
Save messaoudi-mounir/80f5be7c6db46c5b8757 to your computer and use it in GitHub Desktop.
Override Primefaces componant validator (Calendar mindate/maxdate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commons.web.primefaces.component.calendar; | |
import commons.web.jsf.validator.CalendarDateRangeValidator; | |
public class Calendar extends org.primefaces.component.calendar.Calendar { | |
public Calendar() { | |
super(); | |
this.addValidator(new CalendarDateRangeValidator()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package commons.web.jsf.validator; | |
import java.util.Date; | |
import java.util.Map; | |
import javax.faces.application.FacesMessage; | |
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 org.primefaces.component.calendar.Calendar; | |
import org.primefaces.validate.ClientValidator; | |
import dz.gov.mesrs.sii.commons.web.util.CommonMessagesUtils; | |
@FacesValidator(CalendarDateRangeValidator.VALIDATOR_ID) | |
public class CalendarDateRangeValidator implements Validator, ClientValidator { | |
public static final String VALIDATOR_ID = "primefaceDateRangeValidator"; | |
@Override | |
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { | |
if (!(component instanceof Calendar)) | |
return; | |
Date date = (Date) value; | |
if (date == null) | |
return; | |
Date mindate = (Date) component.getAttributes().get("mindate"); | |
Date maxdate = (Date) component.getAttributes().get("maxdate"); | |
if (mindate != null && maxdate != null) { | |
if (mindate.after(date) || maxdate.before(date)) { | |
throw new ValidatorException(CommonMessagesUtils.newBundledFacesMessage(FacesMessage.SEVERITY_ERROR, | |
"", "error_date_validator.date_range", ((Calendar) component).getLabel(), mindate, maxdate)); | |
} | |
} | |
else if (mindate.after(date)) { | |
throw new ValidatorException(CommonMessagesUtils.newBundledFacesMessage(FacesMessage.SEVERITY_ERROR, "", | |
"error_date_validator.min_date", ((Calendar) component).getLabel(), mindate)); | |
} | |
else if (maxdate != null) { | |
if (maxdate.before(date)) { | |
throw new ValidatorException(CommonMessagesUtils.newBundledFacesMessage(FacesMessage.SEVERITY_ERROR, | |
"", "error_date_validator.max_date", ((Calendar) component).getLabel(), maxdate)); | |
} | |
} | |
} | |
@Override | |
public Map<String, Object> getMetadata() { | |
return null; | |
} | |
@Override | |
public String getValidatorId() { | |
return VALIDATOR_ID; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<faces-config version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" | |
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd"> | |
<application> | |
<!-- pour fixer le probleme du validateur mindate/maxdate dans le composant primefaces Calendar --> | |
<component> | |
<component-type>org.primefaces.component.Calendar</component-type> | |
<component-class>commons.web.primefaces.component.calendar.Calendar</component-class> | |
</component> | |
</faces-config> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment