It's all about the answers!

Ask a question

Validator javascript to validate date has syntax error


Don Yang (7.0k2185116) | asked Aug 30 '16, 6:17 a.m.
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.

Accepted answer


permanent link
Donald Nong (14.3k210) | answered Aug 30 '16, 9:12 p.m.
edited Aug 30 '16, 9:12 p.m.
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
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.
Don Yang selected this answer as the correct answer

Comments
Don Yang commented Aug 31 '16, 1:36 a.m.

Thanks Donald.
I tested that before and just now and got the same problem on my machine.
Tried to clear server and broswer cache and different browser, browser debug always returns the same error. I then tried with different version and it works. As it works on your 502, I believe something is wrong with my machine's v5.0.2.


Davyd Norris commented Apr 15, 3:01 a.m. | edited Apr 15, 3:03 a.m.
I know this is quite old, but I am also seeing the same errors in the ccm server logs from my customisations - however my scripts are working fine as far as I can see.If you look carefully  at the error though:

com.ibm.team.rtc.common.scriptengine.UnknownTypeException: 'org.example.DateValidator' is not a constructor

you can see that these errors are generated when the JavaScript engine on the **server** tries to execute the script. The same script is also executed in the web client and this is when it works.

So this appears to be some sort of bug in the RTC JavaScript engine, or perhaps an older version of the engine is running in the server compared to the various web clients?

One other answer



permanent link
Dinesh Kumar (4.0k413) | answered Aug 30 '16, 7:32 a.m.
JAZZ DEVELOPER
edited Aug 30 '16, 7:50 a.m.
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

Comments
Don Yang commented Aug 30 '16, 7:32 p.m. | edited Aug 30 '16, 7:33 p.m.

Hi, Dinesh

Thank you very much for pointing that out, that's my silly mistake. console.log was added to see if I can find any clues why the code does not work as expected. So the code still does not work even though syntax error is not seen now. I still see
'org.example.DateValidator' is not a constructor
error in the log, and when I add a fixed date to be earlier than the creation date, I can save the work item which means the validation is not properly done on the custom attribute. Did you happen to try the code and do you see the logic work in your environment?
I am wondering what else is a problem in this code.

Your answer


Register or to post your answer.