Validator javascript to validate date has syntax error
I am using RTC 5.0.2/Eclipse client 5.0.2 as well.
Referring to https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_Based_Validation
and I tried the below javascript to validate the custom attribute FixedDate(timestamp type) to be set later than creation date.
dojo.provide("org.example.DateValidator");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
//dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
//var Severity = com.ibm.team.workitem.api.common.Severity;
var Status = com.ibm.team.workitem.api.common.Status;
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("org.example.DateValidator", null, {
validate: function(attributeId, workItem, configuration) {
//custom attribute FixedDate has id fixeddate
var fixdate=dojo.date.stamp.fromISOString(workItem.getValue("fixeddate"));
var createdate=dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.CREATION_DATE));
// Compare the two dates and make sure fixeddate is not earlier than creation date
if (dojo.date.compare(fixdate, createdate) >= 0) {
console.log("Fixed date set >= create date");
return Status.OK_STATUS;
} else {
console.date(""Fixed date must be set earlier");
return new Status("ERROR","Fixed date must be set later than Creation Date");
}
}
});
})();
From source code, I see
<customAttributes category="com.ibm.team.workitem.workItemType.task">
<customAttribute id="fixeddate" name="FixedDate" type="timestamp"/>
</customAttributes>
<presentation kind="com.ibm.team.workitem.kind.separator"/>
<presentation attributeId="fixeddate" kind="com.ibm.team.workitem.kind.timestamp"/>
<attributeDefinition id="fixeddate" name="FixedDate" type="timestamp">
<validator providerId="com.ibm.team.workitem.valueproviders.VALIDATOR._XeBK0G54EeaYaaCv-IsPKg"/>
<dependsOn id="com.ibm.team.workitem.attribute.creationdate"/>
</attributeDefinition>
I tried to test the code but the log always shows:
syntax error and
!MESSAGE Error invoking validator com.ibm.team.workitem.valueproviders.VALIDATOR._XeBK0G54EeaYaaCv-IsPKg
!STACK 0
com.ibm.team.rtc.common.scriptengine.UnknownTypeException: 'org.example.DateValidator' is not a constructor
I went through the code many times but could not pinpoint where the syntax error is from.
Can anyone please shed some lights on this error? Thank you very much.
Referring to https://jazz.net/wiki/bin/view/Main/AttributeCustomization#Script_Based_Validation
and I tried the below javascript to validate the custom attribute FixedDate(timestamp type) to be set later than creation date.
dojo.provide("org.example.DateValidator");
dojo.require("com.ibm.team.workitem.api.common.WorkItemAttributes");
//dojo.require("com.ibm.team.workitem.api.common.Severity");
dojo.require("com.ibm.team.workitem.api.common.Status");
dojo.require("dojo.date"); // We need the date class from Dojo to compare two dates
dojo.require("dojo.date.stamp"); // We need the stamp class to work with ISO date strings
(function() {
//var Severity = com.ibm.team.workitem.api.common.Severity;
var Status = com.ibm.team.workitem.api.common.Status;
var WorkItemAttributes= com.ibm.team.workitem.api.common.WorkItemAttributes;
dojo.declare("org.example.DateValidator", null, {
validate: function(attributeId, workItem, configuration) {
//custom attribute FixedDate has id fixeddate
var fixdate=dojo.date.stamp.fromISOString(workItem.getValue("fixeddate"));
var createdate=dojo.date.stamp.fromISOString(workItem.getValue(WorkItemAttributes.CREATION_DATE));
// Compare the two dates and make sure fixeddate is not earlier than creation date
if (dojo.date.compare(fixdate, createdate) >= 0) {
console.log("Fixed date set >= create date");
return Status.OK_STATUS;
} else {
console.date(""Fixed date must be set earlier");
return new Status("ERROR","Fixed date must be set later than Creation Date");
}
}
});
})();
From source code, I see
<customAttributes category="com.ibm.team.workitem.workItemType.task">
<customAttribute id="fixeddate" name="FixedDate" type="timestamp"/>
</customAttributes>
<presentation kind="com.ibm.team.workitem.kind.separator"/>
<presentation attributeId="fixeddate" kind="com.ibm.team.workitem.kind.timestamp"/>
<attributeDefinition id="fixeddate" name="FixedDate" type="timestamp">
<validator providerId="com.ibm.team.workitem.valueproviders.VALIDATOR._XeBK0G54EeaYaaCv-IsPKg"/>
<dependsOn id="com.ibm.team.workitem.attribute.creationdate"/>
</attributeDefinition>
I tried to test the code but the log always shows:
syntax error and
!MESSAGE Error invoking validator com.ibm.team.workitem.valueproviders.VALIDATOR._XeBK0G54EeaYaaCv-IsPKg
!STACK 0
com.ibm.team.rtc.common.scriptengine.UnknownTypeException: 'org.example.DateValidator' is not a constructor
I went through the code many times but could not pinpoint where the syntax error is from.
Can anyone please shed some lights on this error? Thank you very much.
Accepted answer
There are a few problems with the implementation.
1. Double double-quotes, as mentioned earlier.
2. console.date(), also as mentioned earlier.
3. The last statement should have been
4. If you only see the error right after entering the value for "Fixed Date", but submission still goes ahead, make sure that you add the "Attribute Validation" precondition in the Operational Behavior section.
1. Double double-quotes, as mentioned earlier.
2. console.date(), also as mentioned earlier.
3. The last statement should have been
return new Status(Severity["ERROR"],"Fixed date must be set later than Creation Date");Of course you need to uncomment the two related lines to make it work. The Severity class seems to have been deliberately taken out, why?
4. If you only see the error right after entering the value for "Fixed Date", but submission still goes ahead, make sure that you add the "Attribute Validation" precondition in the Operational Behavior section.
Comments
One other answer
Hi Don,
The following line has a syntax error, the double double quotes at the start :)
console.date(""Fixed date must be set earlier");
also, it should be
console.log
instead of
console.date
isn't it!!!
hope this helps.
Regards
Dinesh
The following line has a syntax error, the double double quotes at the start :)
console.date(""Fixed date must be set earlier");
also, it should be
console.log
instead of
console.date
isn't it!!!
hope this helps.
Regards
Dinesh