validation.xslt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed Materials - Property of IBM
  4. IBM Cognos Products: ps
  5. (C) Copyright IBM Corp. 2005, 2013
  6. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. -->
  8. <!--
  9. Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  10. Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  11. -->
  12. <xsl:stylesheet version="1.0"
  13. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  14. xmlns:out="dummy-uri"
  15. xmlns:xts="http://developer.cognos.com/schemas/xts/"
  16. xmlns:xtsext="xalan://com.cognos.xts.ext.XTSExt">
  17. <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
  18. <xsl:namespace-alias stylesheet-prefix="out" result-prefix="xsl"/>
  19. <!-- ======================================================== -->
  20. <!-- Variables -->
  21. <!-- ======================================================== -->
  22. <!-- NOTE:
  23. To prevent errors from showing up in the Tomcat window when not using the pre-compiled templates, client
  24. side validation has been forced off in the version of validation.xslt installed in the templates directory.
  25. To re-enable validation, do the following:
  26. 1. Replace the value of the below rulesFile variable with the following select value:
  27. select="document( 'cafrules.xml' )/cafrules/variables"
  28. 2. Place cafrules.xml in templates/ps and templates/ps/prompting
  29. -->
  30. <xsl:variable name="rulesFile"><noop/></xsl:variable>
  31. <xsl:variable name="lcletters">abcdefghijklmnopqrstuvwxyz</xsl:variable>
  32. <xsl:variable name="ucletters">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
  33. <!-- ======================================================== -->
  34. <!-- Applies validation rules to the form field -->
  35. <!-- ======================================================== -->
  36. <xsl:template name="applyValidation">
  37. <xsl:param name="name"/>
  38. <!-- We need the field name in lower case -->
  39. <xsl:variable name="fieldName">
  40. <xsl:value-of select="translate($name,$ucletters,$lcletters)"/>
  41. </xsl:variable>
  42. <!-- Find the parameter in the caf rules file -->
  43. <xsl:variable name="theRule" select="$rulesFile/variable[@name=$fieldName]"/>
  44. <xsl:if test="$theRule">
  45. <!-- Set a maxlen on the field if it's not already there -->
  46. <xsl:if test="$theRule/@maxlen">
  47. <xsl:if test="not(@maxlength)">
  48. <xsl:attribute name="maxlength">
  49. <xsl:value-of select="$theRule/@maxlen"/>
  50. </xsl:attribute>
  51. </xsl:if>
  52. </xsl:if>
  53. <!--
  54. If it is a int or uint and no maxlength is set: set it to 32:
  55. -->
  56. <xsl:if test="$theRule/@type = 'int' or $theRule/@type = 'uint'">
  57. <xsl:if test="not(@maxlength) and not($theRule/@maxlen)">
  58. <xsl:attribute name="maxlength">32</xsl:attribute>
  59. </xsl:if>
  60. </xsl:if>
  61. <!-- Add a call for this validation function -->
  62. <xsl:variable name="validationFunction">
  63. <xsl:call-template name="validationFunctionName">
  64. <xsl:with-param name="fieldName" select="$fieldName"/>
  65. </xsl:call-template>
  66. </xsl:variable>
  67. <!-- if there was an onblur handler passed in then enhance it with validation checking otherwise add a new onblur handler that does the validation -->
  68. <!-- BUG FIX - 541871.0; Capture the key strokes to prevent clearing input fields when tabbing to Cancel or Close key (KeyDownHandler)-->
  69. <out:variable name="handler">
  70. <BlurHandler>
  71. <xsl:choose>
  72. <xsl:when test="not(@onblur)">javascript:<xsl:value-of select="xtsext:javascriptencode(string($validationFunction))"/>(this);</xsl:when> <!-- SEC_INFO: No threat. $validationFunction is based on input/textarea names within the template, and it is only used if a matching rule name is found in the CAF rules -->
  73. <xsl:otherwise><xsl:variable name="oldOnBlur" select="@onblur"/><xsl:value-of select="$oldOnBlur"/>;<xsl:value-of select="xtsext:javascriptencode(string($validationFunction))"/>(this);</xsl:otherwise> <!-- SEC_INFO: No threat. $validationFunction is based on input/textarea names within the template, and it is only used if a matching rule name is found in the CAF rules -->
  74. </xsl:choose>
  75. </BlurHandler>
  76. <KeyDownHandler>javascript:setGlobalKeyDownHandler(event);</KeyDownHandler>
  77. </out:variable>
  78. <!-- if the browser is ie then we need to add the onblur handler as an onfocusout handler so that we can check to see if the target of the onblur is a cancel or close button -->
  79. <out:choose>
  80. <out:when test="$browser = 'ie'">
  81. <out:attribute name="onkeydown">
  82. <out:value-of select="$handler/KeyDownHandler"/>
  83. </out:attribute>
  84. <out:attribute name="onfocusout">
  85. <out:value-of select="$handler/BlurHandler"/>
  86. </out:attribute>
  87. </out:when>
  88. <out:otherwise>
  89. <out:attribute name="onblur">
  90. <out:value-of select="$handler/BlurHandler"/>
  91. </out:attribute>
  92. </out:otherwise>
  93. </out:choose>
  94. </xsl:if>
  95. </xsl:template>
  96. <xsl:template name="createValidationFunction">
  97. <xsl:param name="name"/>
  98. <xsl:param name="validateobject" select="''"/>
  99. <xsl:variable name="fieldName">
  100. <xsl:value-of select="translate($name,$ucletters,$lcletters)"/>
  101. </xsl:variable>
  102. <!-- Find the parameter in the caf rules file -->
  103. <xsl:variable name="theRule" select="$rulesFile/variable[@name=$fieldName]"/>
  104. <xsl:if test="$theRule">
  105. <xsl:variable name="validationFunction">
  106. <xsl:call-template name="validationFunctionName">
  107. <xsl:with-param name="fieldName" select="$fieldName"/>
  108. </xsl:call-template>
  109. </xsl:variable>
  110. function <xsl:value-of select="xtsext:javascriptencode(string($validationFunction))"/>(field) <!-- SEC_INFO: No threat. $validationFunction is based on input/textarea names within the template, and it is only used if a matching rule name is found in the CAF rules -->
  111. {
  112. <out:if test="$browser = 'ie'">
  113. if (cafGKeyPress != 9 &amp;&amp; event.toElement &amp;&amp; (event.toElement.id == "IDS_CANCEL" || event.toElement.id == "IDS_CLOSE" || event.toElement.id == "IDS_CLOSE_ICON"<xsl:if test="$validateobject !=''"> || event.toElement.name == "<xsl:value-of select="$validateobject"/>"</xsl:if>))
  114. {
  115. <xsl:if test="$validateobject != ''">
  116. if( event.toElement.name == "<xsl:value-of select="$validateobject"/>")
  117. field.value="";
  118. else
  119. </xsl:if>
  120. field.form.reset();
  121. return;
  122. } else if (cafGKeyPress == 9) {
  123. cafGKeyPress = "";
  124. }
  125. </out:if>
  126. if (window.CAFReportValidationError) {
  127. var value = field.value;
  128. var highlight = null;
  129. <!-- check for invalid strings -->
  130. <xsl:if test="not( $theRule[@skipglobalrule = 'true'] )">
  131. var res = CAFContainsInvalidString(value);
  132. if (res) {
  133. CAFReportValidationError(cafInvalidStringMsg + res);
  134. highlight = res;
  135. }
  136. </xsl:if>
  137. <!-- Check for maximum length. This is needed for textareas as they don't support the maxlength attribute. -->
  138. <xsl:if test="$theRule/@maxlen">
  139. if (value.length &gt; <xsl:value-of select="$theRule/@maxlen"/>)
  140. {
  141. CAFReportValidationError(cafInvalidLength);
  142. highlight=value;
  143. }
  144. </xsl:if>
  145. <!-- Check for int/uint types. -->
  146. <xsl:if test="$theRule/@type = 'int'">
  147. if (value != null &amp;&amp; value != "") {
  148. if (!CAFisInteger(value)) {
  149. CAFReportValidationError(cafInvalidInteger);
  150. highlight = value;
  151. }
  152. }
  153. </xsl:if>
  154. <xsl:if test="$theRule/@type = 'uint'">
  155. if (value != null &amp;&amp; value != "") {
  156. if (!CAFisInteger(value,true)) {
  157. CAFReportValidationError(cafInvalidUnsignedInteger);
  158. highlight = value;
  159. }
  160. }
  161. </xsl:if>
  162. <!-- check for int/uint patternrefs -->
  163. <xsl:for-each select="$theRule/patternref">
  164. <xsl:choose>
  165. <xsl:when test="@name = 'type_int'">
  166. if (value != null &amp;&amp; value != "") {
  167. if (!CAFisInteger(value)) {
  168. CAFReportValidationError(cafInvalidInteger);
  169. highlight = value;
  170. }
  171. }
  172. </xsl:when>
  173. <xsl:otherwise>
  174. <xsl:if test="@name = 'type_uint'">
  175. if (value != null &amp;&amp; value != "") {
  176. if (!CAFisInteger(value,true)) {
  177. CAFReportValidationError(cafInvalidUnsignedInteger);
  178. highlight = value;
  179. }
  180. }
  181. </xsl:if>
  182. </xsl:otherwise>
  183. </xsl:choose>
  184. </xsl:for-each>
  185. if (highlight != null) {
  186. cafValidation_field = field;
  187. cafValidation_highlight = highlight;
  188. //use the setTimout to fix the following event conflicts
  189. //in FF: clicking tab selects the next field,
  190. //in IE: message is displayed twice when the next field is a select element
  191. setTimeout("CAFSelectText(cafValidation_field, cafValidation_highlight);",0);
  192. }
  193. }
  194. }
  195. </xsl:if>
  196. </xsl:template>
  197. <xsl:template name="validationFunctionName">
  198. <xsl:param name="fieldName"/>
  199. <xsl:value-of select="concat('caf_', $fieldName)"/>
  200. </xsl:template>
  201. <!-- ======================================================== -->
  202. <!-- Form handler -->
  203. <!-- ======================================================== -->
  204. <xsl:template match="form">
  205. <xsl:copy>
  206. <xsl:copy-of select="@*"/>
  207. <xsl:apply-templates/>
  208. <script language="javascript" src="{'{$webRoot}'}/cr1/caf.js"/>
  209. <script language="javascript" src="{'{$webcontent}'}/portal/js/validation.js"/>
  210. <script language="javascript">
  211. var cafInvalidStringMsg = &quot;<xts:string id="IDS_VAL_ERR_INVALID_STRING" encode="javascript"/>&quot;;
  212. var cafInvalidInteger = &quot;<xts:string id="IDS_VAL_ERR_INVALID_INTEGER" encode="javascript"/>&quot;;
  213. var cafInvalidUnsignedInteger = &quot;<xts:string id="IDS_VAL_ERR_UNSIGNED_INTEGER" encode="javascript"/>&quot;;
  214. var cafInvalidLength = &quot;<xts:string id="IDS_VAL_ERR_MAXIMUM_LENGTH" encode="javascript"/>&quot;;
  215. <out:if test="$browser = 'ie'">
  216. var cafGKeyPress = &quot;&quot;
  217. function setGlobalKeyDownHandler(event)
  218. {
  219. cafGKeyPress = event.keyCode;
  220. }
  221. </out:if>
  222. </script>
  223. </xsl:copy>
  224. </xsl:template>
  225. <!-- ======================================================== -->
  226. <!-- Handlers for individual form fields -->
  227. <!-- ======================================================== -->
  228. <xsl:template match="input[@type='text']">
  229. <!-- validateobject contains the object that focus can be set to without the validation error being triggered (only an issue with IE) -->
  230. <xsl:variable name="validateobject" select="@validateobject"/>
  231. <script language="javascript">
  232. <xsl:call-template name="createValidationFunction">
  233. <xsl:with-param name="name" select="@name"/>
  234. <xsl:with-param name="validateobject" select="$validateobject"/>
  235. </xsl:call-template>
  236. </script>
  237. <xsl:copy>
  238. <xsl:copy-of select="@*[not(name()='validateobject')]"/>
  239. <xsl:call-template name="applyValidation">
  240. <xsl:with-param name="name" select="@name"/>
  241. </xsl:call-template>
  242. <xsl:apply-templates/>
  243. </xsl:copy>
  244. </xsl:template>
  245. <xsl:template match="textarea">
  246. <!-- validateobject contains the object that focus can be set to without the validation error being triggered (only an issue with IE) -->
  247. <xsl:variable name="validateobject" select="@validateobject"/>
  248. <script language="javascript">
  249. <xsl:call-template name="createValidationFunction">
  250. <xsl:with-param name="name" select="@name"/>
  251. <xsl:with-param name="validateobject" select="$validateobject"/>
  252. </xsl:call-template>
  253. </script>
  254. <xsl:copy>
  255. <xsl:copy-of select="@*[not(name()='validateobject')]"/>
  256. <xsl:call-template name="applyValidation">
  257. <xsl:with-param name="name" select="@name"/>
  258. </xsl:call-template>
  259. <xsl:apply-templates/>
  260. </xsl:copy>
  261. </xsl:template>
  262. <xsl:template match="*">
  263. <xsl:copy>
  264. <xsl:copy-of select="@*"/>
  265. <xsl:apply-templates/>
  266. </xsl:copy>
  267. </xsl:template>
  268. </xsl:stylesheet>