Creating your own Form Validator plugin

Form validators are used to implement complex logic that must be evaluated from server side logic and are not covered by the default Metadata Validator element.

To create your own Form validator it is nessesary to create a new class that implements the FieldValidator interface:

package com.openkm.plugin.form;

import java.util.List;

import net.xeoh.plugins.base.Plugin;

/**
 * FieldValidator
 */
public interface FieldValidator extends Plugin {
	public abstract String getName();
	public abstract String validate(String value, List<String> uuids);
}

The new class must be loaded into the package com.openkm.plugin.form because the application plugins system will load it from there. See the sample below:

Do not miss the tag @PluginImplementation otherwise the application plugin system will not be able to retrieve the new class.

More information at: Register a new plugin.

Methods description

MethodTypeDescription

getName()

String

The method returns the name of the plugin used in the form list selector.

validate(String value, List<String> uuids)

String

When the returned string is equal to "", application considers everything to have gone correctly, otherwise error text is returned.

The value variable contains the value of the metadata field and the uuids are the list of the uuid nodes (documents, folders, mails, records) affected.

In the common case that only one node will be updated, the list will only contains one uuid. In case multiple nodes selected it will contain the list of all selected nodes where metadata changes will be applied.

Example of the Form validator implementation

In this example two equal comments in the metadata field named okp:tecnology.comment will not be allowed 

package com.openkm.plugin.form;

import com.openkm.api.OKMRelation;
import com.openkm.bean.Relation;
import com.openkm.dao.LegacyDAO;
import net.xeoh.plugins.base.annotations.PluginImplementation;

import java.util.ArrayList;
import java.util.List;

@PluginImplementation
public class DuplicateDocumentNumberValidator implements FieldValidator {

	@Override
	public String getName() {
		return "Duplicated document number";
	}

	@Override
	public String validate(String value, List<String> uuids) {
		String validate = "";

		String sql = "SELECT NPG_NODE FROM OKM_NODE_PROPERTY WHERE NPG_NAME = 'okp:tecnology.comment' AND NPG_VALUE = " + value;
		try {
			List<List<String>> result = LegacyDAO.executeSQL(sql);
			if (result.size() > 0) {
				if(uuids.isEmpty()) {
					validate = "Duplicated document number";
				} else {
					List<String> allowedUuids = new ArrayList<>();
					for(String uuid: uuids) {
						allowedUuids.add(uuid);
						List<Relation> relations = OKMRelation.getInstance().getRelations(null, uuid);
						for(Relation relation: relations){
							allowedUuids.add(relation.getNode());
						}
					}

					for(List<String> resultList : result){
						if(!allowedUuids.contains(resultList.get(0))){
							validate = "Duplicated document number";
						}
					}
				}
			}
		} catch (Exception e) {
			validate = e.getMessage();
		}

		return validate;
	}
}