{"id":764,"date":"2013-06-20T23:27:57","date_gmt":"2013-06-20T21:27:57","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=764"},"modified":"2013-06-20T23:27:57","modified_gmt":"2013-06-20T21:27:57","slug":"xpages-create-your-own-required-validators","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=764","title":{"rendered":"XPages: Create your own Required Validators"},"content":{"rendered":"<p>If you try to implement your own JSF Validator (by implementing <a title=\"oracle.com: javax.faces.validator.Validator\" href=\"http:\/\/docs.oracle.com\/cd\/E17824_01\/dsc_docs\/docs\/jscreator\/apis\/jsf\/javax\/faces\/validator\/Validator.html\" target=\"_blank\"><em>javax.faces.validator.Validator<\/em><\/a>), you will notice that you are unable to check for an empty value. The reason for this is rather simple: The method <em>validate()<\/em> is only called, if there is something to do. If the value is empty, there is nothing to do, that&#8217;s why nothing happens.<\/p>\n<p>But wait! What about the available required validator for XPages?<\/p>\n<pre><code>&lt;xp:inputText id=\"inputText1\"&gt;\r\n   &lt;xp:this.validators&gt;\r\n\u00a0\u00a0    &lt;xp:validateRequired&gt;&lt;\/xp:validateRequired&gt;\r\n\u00a0\u00a0 &lt;\/xp:this.validators&gt;\r\n&lt;\/xp:inputText&gt;<\/code><\/pre>\n<p>And yes, this is a validator, but the IBM implemented a workaround for their UIComponents: If the UIComponents <em>required<\/em> property is set to <em>true<\/em>, the UIComponent knows that she needs to be filled in. Or a validator has to be attached to the UIComponent which implements the <em>com.ibm.xsp.validator.FacesRequiredValidator<\/em> interface.<\/p>\n<p>Here is an example:<\/p>\n<pre><code>package ch.hasselba.xpages.core;\r\n\r\nimport javax.faces.component.UIComponent;\r\nimport javax.faces.context.FacesContext;\r\nimport javax.faces.validator.ValidatorException;\r\nimport com.ibm.xsp.validator.FacesRequiredValidator;\r\n\r\npublic class RequiredValidator implements FacesRequiredValidator {\r\n\r\n\u00a0\u00a0 \u00a0public String getRequiredMessage() {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return \"Error! Required field is empty!\";\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0public void validate(FacesContext facesContext, UIComponent uiComponent,\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0Object obj) throws ValidatorException {\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/code><\/pre>\n<p>When processing the UIComponent data, the attached validators are checked if they are an instance of <em>FacesRequiredValidator<\/em>, and this is the reason why you cannot attach this validator to a component in the DDE directly: Because if you define the validator in the <em>faces-config.xml<\/em> and add it to the UIComponent with the validator id&#8230;<\/p>\n<pre><code>&lt;xp:inputText id=\"inputText1\"&gt;\r\n   &lt;xp:this.validators&gt;\r\n\u00a0\u00a0    &lt;xp:validator validatorId=\"myValidator\" \/&gt;\r\n   &lt;\/xp:this.validators&gt;\r\n&lt;\/xp:inputText&gt;<\/code><\/pre>\n<p>&#8230; the XPages engine attaches a validator which is not implementing the <em>FacesRequiredValidator<\/em> interface. Instead, an instance of <em>com.ibm.xsp.validator.ValidatorImpl <\/em>is attached.<\/p>\n<p>You have to do this by your own, for example in the <em>beforeRenderResponse<\/em> event:<\/p>\n<pre><code>\r\n&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;xp:view xmlns:xp=\"http:\/\/www.ibm.com\/xsp\/core\"&gt;\r\n\r\n   &lt;xp:this.beforeRenderResponse&gt;\r\n      &lt;![CDATA[#{javascript:getComponent('inputText1').addValidator(\r\n         new ch.hasselba.xpages.core.RequiredValidator()\r\n      );}]]&gt;\r\n   &lt;\/xp:this.beforeRenderResponse&gt;\r\n\r\n   &lt;xp:button\r\n      value=\"Label\"\r\n      id=\"button1\"&gt;\r\n      &lt;xp:eventHandler event=\"onclick\" submit=\"true\"\r\n         refreshMode=\"complete\" immediate=\"false\" save=\"true\" \/&gt;\r\n   &lt;\/xp:button&gt;\r\n\r\n   &lt;xp:br \/&gt;\r\n\r\n   &lt;xp:messages id=\"messages1\"&gt;&lt;\/xp:messages&gt;\r\n\r\n   &lt;xp:br \/&gt;\r\n\r\n   &lt;xp:inputText id=\"inputText1\" \/&gt;\r\n\r\n&lt;\/xp:view&gt;\r\n<\/code><\/pre>\n<p><em>[This example is just for a demonstration. It contains a bug, the validator will attached over and over again, so you have to check if the validator is already attached or not]<\/em><\/p>\n<p>If you now open the XPage, your validator will work as expected:<\/p>\n<p><img decoding=\"async\" alt=\"\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAM4AAABmCAIAAAAMMjkQAAADhklEQVR4nO2bMW7bMBSGOdSDL2B49WlyAB8hF\/GcwUDG3iJAAY9GfYAu9iUyFgaKoEBTdYhhuxJFURT1kxG\/Dw9BQD9Rj3yfmKSuzXcACSZ1AVAKZgMgwVQZY0zW5UEvLr1Mfbha2Gw2qDYlUA1EoBqIQDUQgWogAtVABKqBiG7VzOOzvx+9kt35qDYxUA1EoBqICFfNPD5fo5bcHHfno1oJBKrW1OjeJ+t4Wz6qFUKcU62vUqhWIPFPtbZxHzVRbcLoVPOfHNUmCb+rgQgv1dp+9t2P1L7yFyjU4I0pEIFqIALVQASqgYhLL8f6QNYwUG1KmKqqTMak3h+IBr0EEagGIlANRKAaiEA1ENGl2o8vBBElPFR7+0oQwwPVCFGgGiEKVCNEgWqEKFCNEEWIapY3xQcW0Zgh+i0c9wq+yqe2tgujVDXGPoxXSaBqYy87h2ZEqcdTtRxWNHZt\/6l2Pp8Ph8Pr62uYah\/jza+V7QCwpjluUbu2eZxcR+5v2ry89o2jQuuczWR3Yc1Xw1bXeYv7Qc8ER0fGiJtqp9NpuVwaY+bz+cvLS5hq1tW2tdya0DnSKdCome40n1c9Z+vclraFeK7U+pw4mjs8bqqt1+ur6avVKuB3tbZtcvegbU+tt+g1m2fvgyuMcjtHmnXn\/evsVZhnAUPiptrDw8O1r4vFIvgHaNxGNl\/1t7xX7\/2fHMeEjkl6rbf2dIXVma9qu91uNpt9rOHp6Skf1TqTh2xx3zk9Jxy43uC0z6FaVVXH43G73e73+yF\/FgSsv\/MW\/rP5H3WxHoYoZocdfv4LCXjSokfMf1dzVF\/L7LWn1vzabPcbas3sPIQcczbHHT1zL7NztrZKAnayVmHzKrF2E3y3QPOMfqLo3JB8T7XMA9V6bYhsuyaoGpFnoBohClQjRIFqhCj4xBQhCj4HCiJQDUSgGohANRCBaiAC1UAEqoEIVAMRqAYiUA1EoBqIQDUQgWogAtVABKqBCFQDEagGIlANRKAaiEA1EGGqqvoGGZDahNG5qPYTklKQamdISkGq\/YKkFKTaGySlINV+Q1IKUu0PJKUg1d4hKQWp9heSUpBqqcsonRJagGpZUEILUC0LSmgBqmVBCS1AtSwooQWolgUltADVsqCEFqBaFpTQAlTLghJawP\/CzYXUJowOny0AEagGIlANRKAaiEA1EIFqIALVQASqgYh\/WRDqdpcrdOIAAAAASUVORK5CYII=\" \/><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you try to implement your own JSF Validator (by implementing javax.faces.validator.Validator), you will notice that you are unable to check for an empty value. The reason for this is rather simple: The method validate() is only called, if there &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=764\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,26,76,74],"tags":[47,14,7,31,86,5,3],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-java","category-jsf","category-ssjs","category-xpages","tag-9-0","tag-designer","tag-domino","tag-java","tag-jsf","tag-ssjs","tag-xpages"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=764"}],"version-history":[{"count":13,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":1240,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/764\/revisions\/1240"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}