Creating a plugin that provides a conditional form validator.
The project of the created plugin is available in the public repository http://192.168.1.61/developers/tutorials. A compiled version can be downloaded here:
We assume that the plugin project has already been created. The description of creating the system’s plugin is given above.
Introduction
This course describes the process of creating a plug-in for the PlusWorkflow system at least 3.1 version. This plugin will provide a form validator which:
- check whether the variable given in the parameter meets the PESEL conditions when the execution validator condition is met
- display a message if the variable is not PESEL
Validator preparation
The first step is to create a validator definition and its validating method. In this way, you inform the process editor that such validator is available and can be used to build a task form. To do this, create a class with the appropriate set of annotations and a validating method:
@Validator public class PeselValidator { private final String PESEL_REGEX = "^\\d{11}$"; @Define public void definition( ValidatorDefinitionBuilder builder ) { builder .id( "pesel-validator" ) .name( "validator.pesel" ) .description( "validator.pesel.desc" ) .category( Categories.TEST ) .parameter() .id( "pesel_param" ) .name( "validator.pesel.parameter.name" ) .description( "validator.pesel.parameter.desc" ) .type( Types.VARIABLE) .create(); } //Validation method public void validate( @Param( value = "pesel_param" ) Variable pesel, ValidationErrors errors, Translator translator ) { boolean isPesel = Pattern.matches( PESEL_REGEX, (String) pesel.getValue() ); if ( isPesel == false ) { errors.add( translator.getMessage( "validator.pesel.invalid" ), pesel.getId() ); } } }
Definition
Above we have defined the pesel-validator
with one text parameter, that here we will want to obtain the variable value for validation. As the names and descriptions of the validator and its parameter, we have provided the keys, as we will want these properties to be translated depending on the language of the user. We add translations in files /resources/locale/messages.properties
(default - Polish version) and /resources/locale/messages_en.properties
(English version).
validator.pesel=Walidacja peselu validator.pesel.desc=Walidator weryfikuje czy podany parametr jest peselem validator.pesel.parameter.name=Pesel validator.pesel.parameter.desc=Wartość do walidacji validator.pesel.invalid=Należy podać prawidłowy PESEL
validator.pesel=Validate pesel validator.pesel.desc=Validator to validate if parameter is pesel validator.pesel.parameter.name=Pesel validator.pesel.parameter.desc=Value to validate validator.pesel.invalid=Fill with valid PESEL
In action definition we have also provided the Categories.TEST. It is necessary to create such category or use category that already exists. The category implementation looks as follows:
public enum Categories implements Category { TEST( "Test" ); private String name; private Categories( String name ) { this.name = name; } @Override public String getName() { return name; } }
Validation method
Despite of validator definition we have also implemented validation method.
Validation method must have name validate
In validation method, we defined the input parameters.
@Param( value = "pesel_param" ) String pesel
- value for the validator defined parameter with idpesel_param
ValidationErrors errors
- object to which validator errors can be savedTranslator translator
- plug-in translator object
When calling the method, to all parameters will be injected defined parameters and auxiliary objects.
All possible parameters that we can use in the validator method are described on the page: Validators creating
The validation method verifies whether the pesel_param parameter is a PESEL number. If it is not, then it adds the translated information about it to the ValidationErrors object and means that it is about the variable given in the parameter.
Plugin configuration
The validator has already been defined. It is necessary to "tell" the system that the plug-in provides the validator and function and uses the translations. The suncode-plugin.xml file should look like this:
<?xml version="1.0" encoding="UTF-8"?> <plugin key="${project.groupId}-${project.artifactId}" name="Pesel Validator Tutorial"> <!-- Translations --> <i18n key="i18n" /> <workflow-components key="components" /> </plugin>
Using validator in task
StThe created validator can be used in the process editor after running the plugin. To check its operation, create a simple process and then add our validator to the acceptance button. To do this, enter the task form, then enter the acceptance edition button and expand the Validators tab. Next, use the button to add the validator.
After adding the validator, a window with its definition will appear. The validator parameter and the calling condition must be filled.
At this point, we added the validator to the button accepting the task, which will verify if the variable Pesel is the PESEL number. Validation will be called only if the variable Name and Last name will not be empty.
The validation result of the incorrect number PESEL is presented below: