Tutorials > Management Center > Add a validation rule in the Marketing tool
Add a new validation rule for a Web activity on the OpenLaszlo client side
In this step, we will add a new validation rule for a Web activity in the Management Center. The validation rule will display an error message if the web activity is not set to be active for 10 days, by comparing the start and end dates.
In the Management Center, validators are implemented as OpenLaszlo classes. All validator classes must extend the wcfValidator class. In this step, we will create a new validation class that compares the start date and the end date. If the time period between the two dates is less than 10 days, the validator will set an error message on the activity. The error message must be translated and should be stored in a properties file. You will also create a properties file to store the message and an OpenLaszlo ResourceBundle class to retrieve the translated message. After creating these classes, we will include them in the extensions library file so that the OpenLaszlo compiler can find them.
Procedure
- Create a new validator class
- Open WebSphere Commerce Developer and switch to the Enterprise Explorer view.
- In the Enterprise Explorer view, navigate to LOBTools > WebContent > WEB-INF > src > lzx.
- Right-click lzx and select New > Folder. In the Folder name field, type the name of your company. This will distinguish the files from the files provided by IBM. For example, type mycompany and click Finish.
- Right-click mycompany and select New > Folder. In the Folder name field, type marketing and click Finish.
- To create a new validation rule source file under the directory marketing, right-click marketing and select New > File. In the File name field, type mycompanyValidator.lzx and click Finish.
- Define the validator by entering the following code in the newly created mycompanyValidator.lzx:
You may replace MyCompany or mycompany with your actual company name that you chose while creating the folder. Note the case sensitivity when making changes, in order to be consistent with the naming conventions.
<library> <!--- This validator is validator of object level. It ensures that the end date must be at least 10 days after the start date if both have values. The start date and end date are determined by the propertyname startdate and enddate . If a validation error is found, an error message with key being endDateMustAfterStartDateOver10days will be added to the object. --> <class name="extMycompanyStartEndDateValidator" extends="wcfValidator"> <!--- Performs validation on startdate and enddate. Call addValidationError and clearValidationError methods to handle the error info. --> <method name="validate" args="o, changedProperty"> <![CDATA[ if(o.getProperty("enddate").value && o.getProperty("startdate").value){ var startDate = this.parseDate(o.getProperty("startdate").value); var endDate = this.parseDate(o.getProperty("enddate").value); var days = Math.floor(endDate.getTime()/(1000*60*60*24)) - Math.floor(startDate.getTime()/(1000*60*60*24)); if (days < 10) { o.getProperty("enddate").addValidationError(this, mycompanyResources.endDateMustAfterStartDateOver10days.string); return false; } } o.getProperty("enddate").clearValidationError(this); ]]> </method> <!--- Parse the specified timestamp and return a Date object. The timestamp format is yyyy-MM-ddTHH:mm:00.001Z. @param string timestamp: the timestamp string @return Date the date object --> <method name="parseDate" args="timestamp"> <![CDATA[ var year = Number(timestamp.substr(0,4)); var month = Number(timestamp.substr(5,2)); var day = Number(timestamp.substr(8,2)); var hour = Number(timestamp.substr(11,2)); var minutes = Number(timestamp.substr(14,2)); var seconds= Number(timestamp.substr(17,2)); var milliseconds = Number(timestamp.substr(20,3)); return new Date(year, month, day, hour, minutes, seconds, milliseconds); ]]> </method> </class> </library>
<library> <!--- This validator is validator of object level. It ensures that the end date must be at least 10 days after the start date if both have values. The start date and end date are determined by the propertyname startdate and enddate . If a validation error is found, an error message with key being endDateMustAfterStartDateOver10days will be added to the object. --> <class name="extMycompanyStartEndDateValidator" extends="wcfValidator"> <!--- Performs validation on startdate and enddate. Call addValidationError and clearValidationError methods to handle the error info. --> <method name="validate" args="o, changedProperty=null"> <![CDATA[ if(o.getProperty("enddate").value && o.getProperty("startdate").value){ var startDate = this.parseDate(o.getProperty("startdate").value); var endDate = this.parseDate(o.getProperty("enddate").value); var days = Math.floor(endDate.getTime()/(1000*60*60*24)) - Math.floor(startDate.getTime()/(1000*60*60*24)); if (days < 10) { o.getProperty("enddate").addValidationError(this, mycompanyResources.endDateMustAfterStartDateOver10days.string); return false; } } o.getProperty("enddate").clearValidationError(this); ]]> </method> <!--- Parse the specified timestamp and return a Date object. The timestamp format is yyyy-MM-ddTHH:mm:00.001Z. @param string timestamp: the timestamp string @return Date the date object --> <method name="parseDate" args="timestamp"> <![CDATA[ var year = Number(timestamp.substr(0,4)); var month = Number(timestamp.substr(5,2)); var day = Number(timestamp.substr(8,2)); var hour = Number(timestamp.substr(11,2)); var minutes = Number(timestamp.substr(14,2)); var seconds= Number(timestamp.substr(17,2)); var milliseconds = Number(timestamp.substr(20,3)); return new Date(year, month, day, hour, minutes, seconds, milliseconds); ]]> </method> </class> </library>
The validation occurs at the object level. It ensures the end date is at least 10 days after the start date, when both have the same values. The start date and the end date are determined by the propertyname, startdate and enddate. If a validation error is found, an error message with endDateMustAfterStartDateOver10days key will be added to the object.
- Click Save to save the changes.
- Create properties files
- In the Enterprise Explorer view, navigate to LOBTools > Java Resources: scr. Right-click src and select New > Package. In the Name field, type com.mycompany.marketing.client.lobtools.properties and click Finish.
- Right-click com.mycompany.marketing.client.lobtools.properties and select New > Other > General > File. In the Name field, type MyMarketingLOB.properties. In the same way, create the MyMarketingLOB_en_US.properties file.
- Translate the message to the other locales that to support and place them in the .properties file for the specific locale that you created in step 2.
- Add the following information to the MyMarketingLOB.properties and MyMarketingLOB_en_US.properties files:
endDateMustAfterStartDateOver10days=Client Warning: The end date must be at least 10 days after the start date.
- Click Save to save the changes.
- Create the ResourceBundle class
- Right-click LOBTools > WebContent > WEB-INF > src > lzx > mycompany > marketing and select New > File. In the File name field, type mycompanyResourceBundle.lzx.
- Add the following code for this ResourceBundle class:
<library> <class name="extMycompanyResourceBundle" extends="wcfResourceBundle" baseName="com.mycompany.marketing.client.lobtools.properties.MyMarketingLOB"> <wcfResourceBundleKey name="endDateMustAfterStartDateOver10days" /> </class> <extMycompanyResourceBundle /> </library>
- Click Save to save the changes.
- Include the classes in the extension library
- Navigate to LOBTools > WebContent > WEB-INF > src > lzx > commerce > marketing and open MarketingExtensionsLibrary.lzx. Insert the following code between the open and close library tags within the file and click Save.
You must include the newly created OpenLaszlo files in the application so that the OpenLaszlo compiler knows about them. Each tool within the Management Center provides an ExtensionsLibrary.lzx for this purpose.
<include href="../../mycompany/marketing/mycompanyResourceBundle.lzx"/> <include href="../../mycompany/marketing/mycompanyValidator.lzx"/>
- Use the new validator
- Navigate to LOBTools > WebContent > WEB-INF > src > lzx > commerce > marketing > objectDefinitions.
- Open the WebActivityPrimaryObjectDefinition.lzx file.
- Find the mktWebActivityPrimaryObjectDefinition class and insert <extMycompanyStartEndDateValidator/> before the </class> tag.
- Click Save to save the changes.
- Navigate to LOBTools > WebContent > config > commerce > marketing > objectDefinitions.
- Open the WebActivityPrimaryObjectDefinition.def file.
- Find the mktBaseWebActivityPrimaryObjectDefinition definition and insert <MycompanyStartEndDateValidator package="ext"/> before the </PrimaryObjectDefinition> tag.
- Click Save to save the changes.
- Select the LOBTools project; then click Build OpenLaszlo Project.