Pravin Reddy
Pravin Reddy

Reputation: 681

Birt Report parameter validation

NewParameter is of type DATE and having Custom Format as MM/dd/yyyy in parameter window.

I have written the following code to verify whether the user is sending correct format or not. If user sends 24/12/2014 the code should throw error as Invalid format. But the following code is throwing the error even while user send the correct format e.g: 11/10/2014

importPackage(Packages.java.lang);
importPackage(Packages.java.text);

sdf = new SimpleDateFormat("MM/dd/yyyy");  
testDate = null;
dateerror=false;

try  {  
    testDate = sdf.parse(params["NewParameter"]);  
    dataerror=false;
} catch (e) {
    errorMessage = "the date you provided is in an invalid date";  
    eee = e;
    dateerror=true; 
    reportContext.getDesignHandle().findElement("mytable").drop();
}  

Upvotes: 0

Views: 1076

Answers (1)

Dominique
Dominique

Reputation: 4332

It seems there is a confusion: when we declare a report parameter as "DATE", we don't have to parse it in report scripts because it is already a java Date object. Therefore this line of code is wrong, because the parser would expect a "String" type:

testDate = sdf.parse(params["NewParameter"]);

The custom format "MM/dd/yyyy" you linked to the report parameter is not actually used by the report engine: it is only taken into consideration by the webviewer, to specify how dates should be displayed in parameters dialog.

The best way to achieve your requirements would be to customize client-side javascript of your webviewer, in order to control date format in web-browsers. An example of this approach allows to enter dates through a calendar such here.

Otherwise if you really need to validate a format during report execution, although it is not very elegant the only way would be to set your parameter as "String" type and put your script in "Initialize" event of the report. However in this case you would have to parse it each time it is used in a dataset, or parse it once in a report variable.

Upvotes: 1

Related Questions