Creatio lets you implement the business logic of the Freedom UI page using validators. Validators are functions that check whether the value of the ViewModel attribute is correct. For example, they can check the value of a record field for compliance with specified conditions.
You can implement a custom validator in the following ways:
- Use the validators section of the Freedom UI page schema. Supported in Creatio 8.0 Atlas and later. View example that implements validator in a separate article: Implement the field value validation on a page.
- Use a remote module. Supported in Creatio 8.0.8 Atlas and later.
To implement a custom validator using remote module:
- Create an Angular project to develop a custom validator using remote module.
- Create a custom validator using remote module.
- Add the validator implemented using remote module to the Freedom UI page.
1. Create an Angular project to develop a custom validator using remote module
Develop a custom validator in a dedicated npm package using an external IDE. This example covers the custom validator development in Microsoft Visual Studio Code.
You can create an Angular project to develop a custom validator in multiple ways, similarly to creating an Angular project to develop a custom UI component using remote module. To do this, follow the instruction in a separate article: Custom UI component implemented using remote module.
2. Create a custom validator using remote module
-
Create an Angular class in the project. To do this, run the ng g class some-validator-name.validator command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the SomeValidatorNameValidator class files to the src/app/ project directory.
-
Implement the validator.
- Open the some-validator-name.validator.ts file.
- Flag the SomeValidatorNameValidator class using the @CrtValidator decorator.
- Inherit the BaseValidator class from the @creatio-devkit/common library.
- Implement the field async.
- Implement the abstract method validate() that validates data in the field.
- Import the required functionality from the libraries into the class.
- Save the file.
-
Inject dependencies.
- Open the some-validator-name.validator.ts file.
- Create the SOME_TOKEN_NAME instance of the InjectionToken class. Use the token to work with Angular DI (dependency injection).
- Mark DI in the constructor of the SomeValidatorNameValidator class using the @CrtInject decorator.
- Import the required functionality from the libraries into the class.
- Save the file.
-
Register the SomeValidatorNameValidator validator as a validator and the SOME_TOKEN_NAME token.
- Open the app.module.ts file.
- Add the SomeValidatorNameValidator validator to the validators section in the CrtModule decorator.
-
Add the configuration object to the providers section in the NgModule decorator.
- Set the provide property to SOME_TOKEN_NAME.
- Set the useValue property to the validation value.
- Implement the resolveDependency() method in the bootstrapCrtModule() method of the AppModule root module. The bootstrapCrtModule() method registers the SomeValidatorNameValidator validator flagged using the CrtModule decorator. The resolveDependency() method receives the dependencies of the SomeValidatorNameValidator converter.
- Import the required functionality from the libraries into the class.
- Save the file.
- Build the project. To do this, run the npm run build command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist directory of the Angular project. The build will have the <%projectName%> name specified on step 1.
3. Add the validator implemented using remote module to the Freedom UI page
- Repeat steps 1-6 of the procedure to implement a custom UI component using remote module.
-
Bind the usr.SomeValidatorNameValidator validator to the model attribute in the viewModelConfig schema section.
- Click Save on the client Module Designer’s toolbar.
As a result, Creatio will apply the validator to the element on the Freedom UI page.
1. Create an Angular project to develop a custom validator using remote module
To create an Angular project to develop a custom validator using remote module, follow the instruction in a separate article: Implement custom UI component using remote module.
2. Create a custom validator using remote module
-
Create an Angular class in the project. To do this, run the ng g class verify-request-name.validator command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the VerifyRequestNameValidator class files to the src/app/ project directory.
-
Implement the validator.
- Open the verify-request-name.validator.ts file.
- Flag the VerifyRequestNameValidator class using the @CrtValidator decorator.
- Inherit the BaseValidator class from the @creatio-devkit/common library.
- Implement the field async.
- Implement the abstract method validate() that validates data in the field.
- Import the required functionality from the libraries into the class.
- Save the file.
-
Inject dependencies.
- Open the verify-request-name.validator.ts file.
- Create the REQUEST_NAME_PREFIX instance of the InjectionToken class. Use the token to work with Angular DI (dependency injection).
- Mark DI in the constructor of the VerifyRequestNameValidator class using the @CrtInject decorator.
- Import the required functionality from the libraries into the class.
- Save the file.
Complete source code of the verify-request-name.validator.ts file -
Register the VerifyRequestNameValidator validator as a validator and the REQUEST_NAME_PREFIX token.
- Open the app.module.ts file.
- Add the VerifyRequestNameValidator validator to the validators section in the CrtModule decorator.
-
Add the configuration object to the providers section in the NgModule decorator.
- Set the provide property to REQUEST_NAME_PREFIX.
- Set the useValue property to the REQ.
- Implement the resolveDependency() method in the bootstrapCrtModule() method of the AppModule root module. The bootstrapCrtModule() method registers the VerifyRequestNameValidator validator flagged using the CrtModule decorator. The resolveDependency() method receives the dependencies of the VerifyRequestNameValidator converter.
- Import the required functionality from the libraries into the class.
- Save the file.
Complete source code of the app.module.ts file - Build the project. To do this, run the npm run build command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist directory of the Angular project. The build will have the sdk_remote_module_package name.
3. Add the validator implemented using remote module to the Freedom UI page
-
Set up the Freedom UI page.
- Repeat steps 1-7 of the procedure to implement custom UI component using remote module.
- Click the
button in the action panel of the Freedom UI Designer. After you save the page settings, Creatio opens the source code of the Freedom UI page.
-
Bind the usr.VerifyRequestNameValidator validator to the model attribute in the viewModelConfig schema section.
- Click Save on the Client Module Designer’s toolbar.
As a result, Creatio will add the validator to the Freedom UI page.
Outcome of the example
To view the outcome of the example:
- Open the Requests app page and click Run app.
- Click New on the Requests app toolbar.
- Enter “123” in the Name input.
As a result, Creatio will display an error notification in a pop-up box. The validator is implemented using a remote module created in Angular framework.
