ValidatorValidators are user components that allow you to check a form when accepting a task and block acceptance when some data is not correct. Examples of validators can, for example. - verify that the amount has been set correctly
- verify that a comment or document has been added to the task
The created validators are available in the process editor and can be easily used in a business process, thus enabling the execution of business logic. Their behavior can be further configured using parameters. Defining a validatorValidators are created based on their definition created by the user. Such a definition must contain the following elements: - Annotation
Javadoc |
---|
displayValue | @Validator |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.annotation.Validator |
---|
| (if the validator is not defined in the plugin, it must come from the com.suncode package) - A public method annotated with
Javadoc |
---|
displayValue | @Define |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.annotation.Define |
---|
| a single parameter Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.ValidatorDefinitionBuilder |
---|
|
- A public method called validate, which is responsible for performing form validation.
The validator can also provide a script that builds the appearance of parameters when it is defined in the PWE. To do this, add another annotation for the class Javadoc |
---|
displayValue | @ComponentsFormScript |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.component.annotation.ComponentsFormScript |
---|
| with the path to the script passed in (from classpath).An example definition is shown below: Code Block |
---|
language | java |
---|
linenumbers | true |
---|
| @Validator
@ComponentsFormScript( "path/example-form.js" )
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();
}
//Metoda walidująca
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() );
}
}
} |
Validate function implementationThe validator must have a validating method named validate. The method can have the following parameter types: - a single validator parameter - the type of the parameter must be consistent with the defined type and must be preceded by an annotation
Javadoc |
---|
displayValue | @Param |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.annotation.Param |
---|
| , - parameter mapping two array parameters (key, value) - the type of the parameter must be of type java.util.Map<K,V>, where K is the defined type of the component parameter being the key, and V is the defined type of the component parameter being the value for the key. The parameter must be annotated
Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.annotation.PairedParam |
---|
| with defined properties (key - id of the parameter being the key, value - id of the parameter being the value). - a single parameter of the type
Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.datasource.DataSourceInstance |
---|
| marked with an annotation Javadoc |
---|
displayValue | @Param |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.annotation.Param |
---|
| - if you have such a type, the DataSourceInstance object will be injected based on the value of this parameter, which must be the data source id. Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.Parameters |
---|
| - contains all the defined validator parameters with their values, this is an alternative to retrieving a parameter using the above mentioned annotation Javadoc |
---|
displayValue | @Param |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.component.annotation.Param |
---|
| , Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.error.ValidationErrors |
---|
| - object, in which all errors are set, Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.ValidationContext |
---|
| - validation context, you can read from it the identifier of the process and task affected by this validation, Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.activity.ActivityContextMap |
---|
| - an object that stores the variables of the form with their current values and the process and task identifier; it is entirely read-only, it is not possible to change the values of the variables, Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.component.ContextVariables |
---|
| - context variables available in this validator. More information here, Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.translation.Translator |
---|
| - translator for this component, so that messages returned by the validator can be translated. More information here, Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.administration.user.UserInfo |
---|
| - object containing information about the user accepting the task.
In order to stop acceptance of the task and display the appropriate message, add a message to the object Javadoc |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.error.ValidationErrors |
---|
| . If no message is added, the task acceptance will proceed to the next stage.Registration of the validator in the pluginIf the validator is defined in the plugin, then you need to additionally indicate that the plugin provides components. To do this, add an entry in the suncode-plugin.xml file: Code Block |
---|
| <!-- Sharing validators -->
<workflow-components key="components" /> |
Callback on validator errorIt is possible to add a JavaScript function that will be called instead of displaying a system window when a validator reports an error. If the button has multiple validators, the errors (error boxes, variable underlining, etc.) from the usual validator errors are displayed first, and at that time the validators that reported the callback will be ignored. If the user has gotten rid of all the standard errors, each subsequent acceptance of the task will only call one single callback. To add a callback to a component, next to the annotation Javadoc |
---|
displayValue | Validator |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.annotation.Validator |
---|
| add an annotation Javadoc |
---|
displayValue | ValidatorsScript |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.annotation.ValidatorsScript |
---|
| specifying the path to the script (located in resources). Code Block |
---|
| @Validator
@ValidatorsScript( value = "scripts/peselValidatorCallback.js" )
public class PeselValidator |
To report a callback on the form, call invokeCallback with Javadoc |
---|
displayValue | ValidationErrors |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.error.ValidationErrors |
---|
| . Code Block |
---|
| public void validate( @Param( value = "param" ) Variable param, ValidationErrors errors )
{
//...
if ( validatorFailed )
{
errors.invokeCallback();
}
} |
If the validator simultaneously adds errors and reports a callback invocation, the errors will be ignored by the server (a JavaScript callback will be called). If the validator does not have annotation Javadoc |
---|
displayValue | ValidatorsScript |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.annotation.ValidatorsScript |
---|
| ,an exception will be thrown when invokeCallback is called. The implementation of callback in JavaScript is analogous to the creation of Actions scripts. Code Block |
---|
| PW.FormValidators.create('pesel-validator', {
callback : function() {
console.log('Pesel validator callback');
if(ignorePesel) {
this.confirm();
}
}
}); |
The first argument of the create function must be the same as the id of the validator. The second argument is the object that implements the callback method, which is called when an error is added to the validator. If you want to validate the validator to let the acceptance continue, you must call the validator method this.confirm(). If the validation is called, the callback of the next validator (if any) will automatically be called. Once all the callbacks have been validated, then the acceptance will happen, bypassing these validators. Forwarding an argument to a callback Warning |
---|
The functionality is not available yet. |
The invokeCallback method allows you to forward any object, which can then be used by a browser-side script. This object will be converted to JSON by the Jackson library. Example usage: Code Block |
---|
| Map<String, Object> exampleObject = new HashMap<String, Object>();
exampleObject.put( "prop1", "some text" );
exampleObject.put( "prop2", true );
exampleObject.put( "prop3", 23423 );
errors.invokeCallback( exampleObject ); |
JavaScript side: Code Block |
---|
| callback : function(exampleObject) {
console.log(exampleObject.prop1);
console.log(exampleObject.prop2);
console.log(exampleObject.prop3);
} |
Validation error or confirmation messageIt is possible to specify in the PWE a validation error message and a form acceptance confirmation message despite the validation error. In order to display these fields in the PWE, you need to specify in the validator builder which types of messages can be configured. Code Block |
---|
language | java |
---|
linenumbers | true |
---|
| @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 )
.messageTypes( ValidatorMessage.CONFIRMATION, ValidatorMessage.ERROR ) // it will be possible to configure confirmation message and validation error
.parameter()
.id( "pesel_param" )
.name( "validator.pesel.parameter.name" )
.description( "validator.pesel.parameter.desc" )
.type( Types.VARIABLE)
.create();
}
//Metoda walidująca
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() );
}
}
} |
Enum Javadoc |
---|
displayValue | ValidatorMessage |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.ValidatorMessage |
---|
| determines which messages can be configured in PWE. These messages act as parameters of String type, so it is possible to use values of variables, functions, etc. in them.In the execute method of the validator, it is possible to retrieve two new objects: Javadoc |
---|
displayValue | DefinedConfirmation |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.error.DefinedConfirmation |
---|
| - object storing title and confirmation text Javadoc |
---|
displayValue | DefinedError |
---|
property | javadoc.plusworkflow |
---|
className | com.suncode.pwfl.workflow.form.validator.error.DefinedError |
---|
| - object storing the text of the error
These objects should be added to errors and validation confirmations. Example usage: Code Block |
---|
| public void validate( @Param( value = "param" ) Variable param, ValidationErrors errors,
Translator translator, DefinedError error, DefinedConfirmation confirmation )
{
String value = param.getValue().toString();
if ( /* condition - validation error */ )
{
errors.add( error );
}
else if ( /* condition - despite the error, asking for confirmation of task acceptance */ )
{
errors.addConfirmation( confirmation );
}
} |
|