Is there how to validate into rich:calendar if the date selected is before a specific date?
I have this component:
<rich:开发者_C百科calendar enableManualInput="true" value="#{home.born}" datePattern="dd/MM/yyyy" />
and i need to validate if the selected date is equal or before actual date at the momment... Is there how to do it only with rich:calendar or i must verify it into home?
Problem solved! i've used the solution provided by Balusc. Thanks everybody! :)
To validate it on the server side, you can use a Validator
.
<rich:calendar ...>
<f:validator validatorId="notAfterToday" />
</rich:calendar>
with
@FacesValidator("notAfterToday")
public class NotAfterTodayValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Date date = (Date) value;
Date today = new Date();
if (date.after(today)) {
String message = "Date may not be later than today.";
throw new ValidatorException(new FacesMessage(message));
}
}
}
You should be able to do that using JavaScript (so on client side). If you look on the Client API doc, you can see that there is a getCurrentDate()
JavaScript function provided by the rich:calendar component.
So what you have to do is to launch a JavaScript function on the JavaScript events ondateselected
and oninputchange
that will use the getCurrentDate()
method and compare to the current date.
Something like that (I didn't test):
<h:form id="myForm">
...
<rich:calendar id="myCalendar" enableManualInput="true" value="#{home.born}" datePattern="dd/MM/yyyy"
ondateselected="checkDate();" oninputchange="checkDate();"/>
...
and
<script type="text/javascript">
function checkDate() {
var choosenDate = $("myForm:myCalendar").component.getCurrentDate();
var now = new Date();
// Calculate the difference between the 2 dates.
// This method may be modified if you want to only compare date (i.e. not time).
var diff = choosenDate.getTime() - now.getTime();
if (diff < 0) {
// choosenDate is before today
alert("Error in the selected date!");
// Do what you want here...
}
}
</script>
精彩评论