I have two Date Time Component in my Xpages and I need to compare two date before submit so that I have used an expression validation feature:
<xp:inputText value="#{document1.FUP_creation}"
id="FUP_creation" required="true">
<xp:this.validators>
<xp:validateRequired
message="Date is required">
</xp:validateRequired>
</xp:this.validators>
<xp:dateTimeHelper id="dateTimeHelper3">
</xp:dateTimeHelper>
<xp:this.converter>
<xp:convertDateTime type="date"
dateStyle="short">
</xp:convertDateTime>
</xp:this.converter>
</xp:inputText>
<xp:inputText value="#{document1.FUP_expireDate}"
id="FUP_expiredDate" required="true">
<xp:this.validators>
<xp:validateRequired
message="Date is required">
</xp:validateRequired>
<xp:validateExpression
message="The expired date must be greater than creation date">
<xp:this.expression><![CDATA[#{javascript://_dump("creation date")
_dump(this.getValue())
_dump(this.getSubmittedValue())
var exp_date:java.util.Date=this.getValue();
var fup_creation:java.util.Date=getComponent("FUP_creation").getValue();
if (exp_date.compareTo(fup_creation)>=0) return true
else return false}]]></xp:this.expression>
</xp:validateExpression>
</xp:this.validators>
<xp:dateTimeHelper></xp:dateTimeHelper>
<xp:this.converter>
<xp:convertDateTime type="date"
dateStyle="short">
</xp:convertDateTime>
</xp:this.converter>
</xp:inputText>
The correct value is always getSubmittedValue()..but is return into java.lang.String mode and not java.util.Date.
Now can use java.text.SimpleDateFormat to convert into java.util.Date my submitted value...but this is a correct solution?
You can use the validator validateDateTimeRange
and set minimum
value to your creation date field:
<xp:validateDateTimeRange
message="The expired date must be greater than creation date"
minimum="#{javascript:getComponent('FUP_creation').getValue()}">
</xp:validateDateTimeRange>
That is a nice short solution but it's only useful if both dates can be equal too.
The solution with validateExpression
works this way:
<xp:validateExpression
message="The expired date must be greater than creation date">
<xp:this.expression><![CDATA[#{javascript:
var df:java.text.DateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");
var fup_creation = df.format(getComponent('FUP_creation').getValue());
var exp_date = this.getSubmittedValue();
if(exp_date > fup_creation){
return true;
}else{
return false;}}]]>
</xp:this.expression>
</xp:validateExpression>
this.getSubmittedValue()
returns a String in format "yyyy-MM-dd". That allows us to compare it with a string of same format as the compare operations give the same result as comparing the dates in date format.
Make sure you have a <xp:messages
or <xp:message
tag on your page so that you can see the message.