4.2. Dialog validator

In eXo Platform, you can validate the input element of content template against the declared validation rules by creating your own validator into a jar file, then adding it to the dialog fields.

In this tutorial, it is assumed that you need to create a custom-validator that allows:

Creating a dialog validator

  1. Create a Maven project, for example, named custom-validator, with the following structure:

  2. Edit the java/org/exoplatform/ecm/webui/form/validator/CustomValidator.java file with the following content (Look the sample here):

    package org.exoplatform.ecm.webui.form.validator;
    
    
    import org.exoplatform.web.application.ApplicationMessage;
    import org.exoplatform.webui.core.UIComponent;
    import org.exoplatform.webui.exception.MessageException;
    import org.exoplatform.webui.form.UIForm;
    import org.exoplatform.webui.form.UIFormInput;
    import org.exoplatform.webui.form.validator.Validator;
    public class CustomValidator implements Validator {
      public void validate(UIFormInput uiInput) throws Exception {
        if (uiInput.getValue()==null || ((String)uiInput.getValue()).trim().length()==0) return;
        UIComponent uiComponent = (UIComponent) uiInput ;
        UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class) ;
        String label;
        try{
          label = uiForm.getLabel(uiInput.getName());
        } catch(Exception e) {
          label = uiInput.getName();
        }
        label = label.trim();
        if(label.charAt(label.length() - 1) == ':') label = label.substring(0, label.length() - 1);
        String s = (String)uiInput.getValue();
        Object[] args = { label };
        if (s.length() > 50) {
          throw new MessageException(new ApplicationMessage("CustomValidator.msg.lengthInCorrect", args, ApplicationMessage.WARNING)) ; 
        }
        for(int i = 0; i < s.length(); i ++){
          char c = s.charAt(i);
          if(Character.isDigit(c) || Character.isLetter(c) || c==' ' || c=='.' || c==',') {
            continue ;
          }
          throw new MessageException(new ApplicationMessage("CustomValidator.msg.Invalid-char", args, ApplicationMessage.WARNING)) ;
        }
      }
    }

    In which:

    • The allowed maximum length of the fields that use CustomValidator is 50 characters.

      if (s.length() > 50) {
      
            throw new MessageException(new ApplicationMessage("CustomValidator.msg.lengthInCorrect", args, ApplicationMessage.WARNING)) ; 
          }
    • Only letters, digits, spaces, full stops (.), commas (,) are accepted in the fields that use CustomValidator.

      if(Character.isDigit(c) || Character.isLetter(c) || c==' ' || c=='.' || c==',') {
      
              continue ;
            }
  3. Update the pom.xml file that declares dependencies of the classes imported in the CustomValidator.java file.

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>exo.custom.validator</groupId>
      <artifactId>custom-validator</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>custom-validator</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>org.gatein.portal</groupId>
          <artifactId>exo.portal.component.web.controller</artifactId>
          <version>3.5.9.Final</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.gatein.portal</groupId>
          <artifactId>exo.portal.webui.framework</artifactId>
          <version>3.5.9.Final</version>
          <scope>provided</scope>
        </dependency>
        <dependency>
          <groupId>org.exoplatform.platform-ui</groupId>
          <artifactId>platform-ui-webui-core</artifactId>
          <version>4.0.5</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </project>
  4. Create the language resource for your CustomValidator in resources/locale/portal/custom_validator_en.xml.

    
    <?xml version="1.0" encoding="UTF-8"?>
    <bundle>
    <!--
      ###################################################################################
      # Messages of Custom validator                                                              #
      ###################################################################################
      -->
      <CustomValidator>
        <msg>
            <lengthInCorrect>Your '{0}' length is larger than 50 characters. Please reduce your length.</lengthInCorrect>
            <Invalid-char>The field '{0}' contains some invalid characters. Please enter another value.</Invalid-char>
        </msg>
      </CustomValidator>
    </bundle>
  5. Edit the resources/conf/portal/configuration.xml file to configure ResourceBundleService.

    
    <configuration
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"
       xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd">

        <external-component-plugins>
            <target-component>org.exoplatform.services.resources.ResourceBundleService</target-component>
            <component-plugin>
                <name>ResourceBundle Plugin</name>
                <set-method>addResourceBundle</set-method>
                <type>org.exoplatform.services.resources.impl.BaseResourceBundlePlugin</type>
                <init-params>
                    <values-param>
                        <name>init.resources</name>
                        <value>locale.portal.custom_validator</value>
                    </values-param>
                    <values-param>
                        <name>portal.resource.names</name>
                        <value>locale.portal.custom_validator</value>
                    </values-param>
                </init-params>
            </component-plugin>
        </external-component-plugins>
    </configuration>

    Pay attention to the locale.portal.custom_validator value. It is the translation of the path of your resources (locale/portal/custom_validator) - with the elimination of language code and file extension.

  6. Build the Maven project using the command: mvn clean install.

  7. Put the .jar file (target/custom-validator-1.0-SNAPSHOT.jar) into the lib folder of eXo Platform.

    • $PLATFORM_TOMCAT_HOME/lib (in Tomcat).

    • $PLATFORM_JBOSS_HOME/standalone/deployments/platform.ear!/lib (in JBoss).

  8. Start the eXo Platform server.

Testing

Now you can use your own CustomValidator by adding "validate=org.exoplatform.ecm.webui.form.validator.CustomValidator" to the field content of the dialog that is currently used by the content template.

In this procedure, it is assumed that you want to use CustomValidator in the Name field of the Web Content template.

  1. Go to ContentContent Administration.

  2. In the TemplatesDocuments, click next to the Web Content template to open the View & Edit Template form.

  3. Select the Dialog tab, then click corresponding to the dialog that is currently used by the template (for example, dialog1).

  4. Declare CustomValidator for webContentFieldName in the Content field of the Edit form. For example, replace

    String[] webContentFieldName = ["jcrPath=/node", "nodetype=exo:webContent", "mixintype=mix:votable,mix:commentable,mix:i18n", "editable=if-null","validate=name,empty"] ;

    with

    String[] webContentFieldName = ["jcrPath=/node", "nodetype=exo:webContent", "mixintype=mix:votable,mix:commentable,mix:i18n", "editable=if-null","validate=org.exoplatform.ecm.webui.form.validator.CustomValidator"] ;
  5. Click Save, then go to ContentSites Explorer to open the Sites Management page.

  6. Click New Content, then select the Web Content template.

  7. Try inputting special characters that are not in the list of allowed characters or inputting more than 50 characters in the Name field, then click Save.

    You will see the following warnings.

    • Invalid characters:

    • Incorrect length:

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus