I think most of us would have come across the problem of date validation in XML form builder application. I posted query in SDN as to how to go about this date validation in applications created by XML form builder but with very little response. Here is my blog, which is a simple description of how to handle date validation in the application.
This wiki describes a simple solution to date validation in the application when the save button is clicked. Programmers can come with different solution for the same scenario.
Limitation with XML form builder
XML form builder provides field validation for mandatory fields, Data Types, Data Lengths and Patterns - Strings. I found date validation is not handled in the XML form builder application.
Prerequisite
It is important that you have basic knowledge in XML, XML schema, XML style sheet.
Problem Description
You use a data schema to define XML elements in which the content of your forms is stored. The data schema represents the content structure of your data, and is separate from the structure of your forms.
When an application is build with XML form builder, there are multiple files generated in the KM repository of which data schema and style sheet files (in which we edit the content to enforce date validation) holds important. These files are generated under the /etc/xmlforms folder in the KM repository. The schema and style sheet files for the respective application are found under the name of the application.
For strings, the pattern can be enforced in the schema file
<element name="" default="" minOccurs="1" maxOccurs="1" type="string" ns="default"><simpleType<restriction base="string"><pattern value="^20([0][4-9]|[1][0-9])$"/></restriction></simpleType></element>
This restricts the application to accept the user entry to just values between 2004 and 2009. Such a kind of pattern validation is not possible for the date field in XML form builder, by which you can restrict the date entry. So it becomes imperative to look at the XSL file for performing the above discussed functionality.
Solution
The application has a date field, in which user is not supposed to enter date values less than the current date. To enforce this requirement, I have edited the XSL file.
function HrefSubmit(event, resource){if (event=="save"){var today=new Date(); var expiry = new Date(document.rootform__.field_1212555752540.value);
var difference = today-expiry;
var days = Math.round(difference/(1000*60*60*24);
if (days<0){
document.rootform__.submit_event.value=event;document.rootform__.submit_resource.value=resource;document.rootform__.submit();}
else{document.rootform__.submit_event.value=event;document.rootform__.submit_resource.value=resource;document.rootform__.submit();}}
Under the /etc repository, where the application for the XML form builder is stored, there is an <application>Edit.xsl file. I have edited the javascript method for the save functionality in the application.
The above javascript function is called when the user clicks the save button. field_1212555752540 indicates the expiry date field in the application. Once this javascript is called, the user will be prompted alert message if the entered date value is less than the current value and will not allow the form to be submitted.
Conclusion
As said above, I have handled a simple date validation in my application for demonstration. Programmers can come with a multiple ways to handle this functionality in their own style.