Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Polish

Table of Contents
outlinetrue
stylenone

 

Excerpt

Tworzenie wtyczki udostępniającej warunkowy walidator formularza.

 

Projekt stworzonej wtyczki jest dostępny w publicznym repozytorium http://192.168.1.61/developers/tutorials. Skompilowana wersja do pobrania tutaj:

View file
namepesel-validator-1.0.0.jar
height150

Tip
titlePrzydatne linki
Info
Zakładamy, że projekt wtyczki mamy już utworzony. Opis tworzenia wtyczki systemowej podany jest wyżej.

Wstęp

Kurs ten opisuje proces tworzenia wtyczki dla systemu PlusWorkflow w wersji co najmniej 3.1. Wtyczka ta będzie udostępniała walidator formularza, który:

  • sprawdzi czy podana w parametrze zmienna spełnia warunki PESELu, gdy warunek wykonania walidatora jest spełniony
  • wyświetli wiadomość w przypadku, gdy zmienna nie jest PESELem.

Przygotowanie walidatora

Pierwszym krokiem jest stworzenie definicji walidatora oraz jego metody walidującej. W ten sposób informujemy edytor procesów, że taki walidator jest dostępny i może zostać wykorzystany przy budowie formularza zadania. W tym celu należy stworzyć klasę z odpowiednim zestawem adnotacji oraz metodę walidującą:

Code Block
languagejava
@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();
    }
 
    //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() );
        }
    }
}

Definicja

Powyżej zdefiniowaliśmy walidator pesel-validator z jednym parametrem tekstowym, czyli w tym miejscu będziemy chcieli otrzymać wartość zmiennej do walidacji. Jako nazwy i opisy walidatora i jego parametru podaliśmy klucze, gdyż będziemy chcieli, aby te właściwości były tłumaczone w zależności od języka użytkownika. Tłumaczenia dodajmy w plikach /resources/locale/messages.properties (domyślne - wersja polska) oraz /resources/locale/messages_en.properties (wersja angielska).

Code Block
languagetext
titlemessages.properties
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
Code Block
languagetext
titlemessages_en.properties
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

W definicji akcji podaliśmy również kategorię Categories.TEST. Niezbędne jest stworzenie takiej kategorii lub wykorzystanie kategorii, która już istnieje. Implementacja kategorii wygląda następująco:

Code Block
languagejava
public enum Categories
    implements Category
{
    TEST( "Test" );

    private String name;

    private Categories( String name )
    {
        this.name = name;
    }
 
    @Override
    public String getName()
    {
        return name;
    }
}

Metoda walidująca

Oprócz definicji walidatora zaimplementowaliśmy również metodę walidującą.

Note

Metoda walidującą musi posiadać nazwę validate

W metodzie walidującej zdefiniowaliśmy parametry wejściowe.

  • @Param( value = "pesel_param" ) String pesel - wartość dla zdefiniowanego parametru walidatora o id pesel_param
  • ValidationErrors errors - obiekt, do którego można zapisać błędy walidatora
  • Translator translator - obiekt translatora wtyczki

Podczas wywoływania metody do wszystkich parametrów zostaną wstrzyknięte zdefiniowane parametry i obiekty pomocnicze.

Tip

Wszystkie możliwe parametry które możemy wykorzystać w metodzie walidatora opisane są na stronie: Tworzenie walidatorów

Metoda walidująca weryfikuje czy parametr pesel_param jest numerem PESEL. Jeżeli nie jest, to dodaje tłumaczoną informacje o tym do obiektu ValidationErrors oraz oznacza, że chodzi o zmienną podaną w parametrze.

Konfiguracja wtyczki

Walidator został już zdefiniowany. Należy jeszcze "powiedzieć" systemowi, że wtyczka udostępnia walidator i funkcję oraz korzysta z tłumaczeń. Plik suncode-plugin.xml powinien wyglądać następująco:

Code Block
languagexml
<?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>

Wykorzystanie walidatora w zadaniu

Stworzony walidator będzie mógł być wykorzystany w edytorze procesów po uruchomieniu wtyczki. Aby sprawdzić jego działanie należy stworzyć prosty proces, a następnie dodać nasz walidator do przycisku akceptacji. Aby to zrobić należy wejść w formularz zadania, następnie wejść w edycję przycisku akceptacji i rozwinąć zakładkę Walidatory. Następnie przy pomocy przycisku należy dodać walidator.

Po dodaniu walidatora pojawi się okno z jego definicją. Należy uzupełnić parametr walidatora oraz warunek wywołania.

W tym momencie dodaliśmy walidator do przycisku akceptującego zadanie, który będzie weryfikował czy zmienna Pesel jest numerem PESEL. Walidacja będzie wywoływana tylko jeżeli zmienna Imię i Nazwisko nie będzie pusta.

Poniżej został przedstawiony wynik walidacji błędnego numeru PESEL.

English

Table of Contents
outlinetrue
stylenone

 

Excerpt

Creating a plug-in 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:

View file
namepesel-validator-1.0.0.jar
height150

Tip
titleUseful links
Info

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:

Code Block
languagejava
@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).

Code Block
languagetext
titlemessages.properties
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
Code Block
languagetext
titlemessages_en.properties
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:

Code Block
languagejava
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.

Note

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 id pesel_param
  • ValidationErrors errors - object to which validator errors can be saved 
  • Translator translator - plug-in translator object 

When calling the method, to all parameters will be injected defined parameters and auxiliary objects.

Tip

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:

Code Block
languagexml
<?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: