This article covers a Hippo CMS version 7.7. There's an updated version available that covers our most recent release.

Create a custom field validator 

This functionality is available since Hippo 7.7.3

(CMS version >= 2.22.02 || CMS version >= 2.23.01)

1. First you need to create a validator plugin.

This plugin should extend the org.hippoecm.frontend.editor.validator.plugins.AbstractCmsValidator class.

Example of the new RegExCmsValidator plugin:

http://www.onehippo.org/binaries/sixteencolumn/content/gallery/connect/library/regexcmsvalidator.png

The API requires to implement the following methods:

  • preValidation: Used to check the field type; e.g. if the field is a String type field. (see above example).
  • validate: The method where you perform the check and return the violations. You can retrieve all kinds of information from the arguments e.g.;
  • String field value: (String) childmodel.getObject();
  • Compound type node: ((JcrNodeModel) childModel).getNode();
  • Document node: model.getNode(); 

2. Add the validator plugin to the frontend configuration.

To register the plugin add a new frontend:plugin to the cms-validators plugin cluster. Located at the the following path:

/hippo:configuration/hippo:frontend/cms/cms-validators

Example of the email validator:

http://www.onehippo.org/binaries/sixteencolumn/content/gallery/connect/library/configuration.png

3. Reference the validator from the nodetype (namespace)

Find the document type and the field in the hippo namespace which you'd like to validate.

Example in go green: 

Title of the hippogogreen banner:


Add the node name of the plugin (made in step 2. Add the validator plugin to the frontend configuration.) as a value to the hipposysedit:validator property.

http://www.onehippo.org/binaries/sixteencolumn/content/gallery/connect/library/banner.png

4. I18n & API

The last step is to create the validation message. You can do this in the cms-validators plugin cluster validation translator located at:

/hippo:configuration/hippo:frontend/cms/cms-validators/validationTranslator/hippostd:translations

Add or copy a new translation configuration node with the same name as the plugin (made in step 2. Add the validator plugin to the frontend configuration.) with the

appropriate violationappropria message. 

http://www.onehippo.org/binaries/sixteencolumn/content/gallery/connect/library/translation.png

Note that you can access the translation from your the API with the #getTranslation method and access different translations with the #translateKey method

violations.add(fieldValidator.newValueViolation(childModel, getTranslation()));
...
violations.add(fieldValidator.newValueViolation(childModel, translateKey(FORMNOTVALID)));

Check if your validator works:

http://www.onehippo.org/binaries/sixteencolumn/content/gallery/connect/library/validing.png