Pravin Reddy
Pravin Reddy

Reputation: 681

Date parameter validation in Birt

I am trying to implement the validation to Date parameter in Birt. But script is not executing as expected.

Eg:

ParameterName : StartDate Type : String

  1. Validation for Length :

    Validation script in beforeFactory :

    errorcode=0; var str=params["StartDate"].value; if( str.length != 10 ) { errorcode=1001; }

    HTML Text in Report layout:

    // Error Codes

    var ERR_INVALID_DATE_FORMAT = 1001; var ERR_INVALID_START_DATE_VALUE = 1003; var ERR_INVALID_END_DATE_VALUE = 1004;

    // Error Messages​ var MSG_INVALID_DATE_FORMAT ="Invalid Date Format"; var MSG_INVALID_START_DATE_VALUE ="Invalid start date value"; var MSG_INVALID_END_DATE_VALUE ="Invalid end date value";

    switch(errorcode;) { case ERR_INVALID_DATE_FORMAT: ​ alert( MSG_INVALID_DATE_FORMAT ); break;

    case ERR_INVALID_START_DATE_VALUE: alert( MSG_INVALID_START_DATE_VALUE ); break;

    case ERR_INVALID_END_DATE_VALUE: alert( MSG_INVALID_END_DATE_VALUE ); break;

    default: alert(errorcode); }

The switch statement is not able to validate the errorcode and it's not showing the alert message.

Note: Earlier used custom format for date parameter but it's not able to detect the incorrect values for Date like aa/23/20145.

Is there any way to implement these validation?

Upvotes: 1

Views: 983

Answers (1)

Dominique
Dominique

Reputation: 4332

The main issue here is, your HTML text code is a client-side script therefore it cannot directly access to variable "errorcode" you created in the beforeFactory event. For that you need to make use of "VALUE-OF" tag such below. Notice how this variable "errorcode" is declared in a HTML script:

<script>

// Error Codes
var ERR_INVALID_DATE_FORMAT = 1001, 
    ERR_INVALID_START_DATE_VALUE = 1003, 
    ERR_INVALID_END_DATE_VALUE = 1004,
    MSG_INVALID_DATE_FORMAT ="Invalid Date Format", 
    MSG_INVALID_START_DATE_VALUE ="Invalid start date value",
    MSG_INVALID_END_DATE_VALUE ="Invalid end date value",
    errorcode=<VALUE-OF>errorcode</VALUE-OF>;

console.log("Evaluating date format for code:"+errorcode);  

switch (errorcode) {
    case ERR_INVALID_DATE_FORMAT:
        alert(MSG_INVALID_DATE_FORMAT);
        break; 
    case ERR_INVALID_START_DATE_VALUE:
        alert(MSG_INVALID_START_DATE_VALUE);
        break; 
    case ERR_INVALID_END_DATE_VALUE:
        alert(MSG_INVALID_END_DATE_VALUE);
        break;         
    default: 
        alert("Format seems valid, error code returned is:"+errorcode);
}

</script>

However it is probably a better practice to apply this kind of validation step in a server-side script, in particular it would be easier to maintain. You have an example of a such approach in this topic. Using a javascript calendar as suggested by the topic pointed by James would also improve users experience. I hope it will help.

Upvotes: 2

Related Questions