Custom validator implemented using remote module
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.
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 thesrc/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.
some-validator-name.validator.ts file/* Import the required functionality from the libraries. */
import { BaseValidator, CrtControlState, CrtInject, CrtValidationErrors, CrtValidator, ValidatorParametersValues } from "@creatio-devkit/common";
@CrtValidator({
type: 'usr.SomeValidatorNameValidator',
})
export class SomeValidatorNameValidator extends BaseValidator {
constructor(private _name: string) {
super();
}
/* Implement a field async. */
protected override async = false;
/* Implement the "validate" abstract method. */
public validate(
control: CrtControlState,
params?: ValidatorParametersValues
): CrtValidationErrors | null {
/* Implement the logic of the method. */
const value = control.value as string;
if (value === this._name) {
return {
error: {
message: 'Validation error message'
},
}
}
return null;
}
} - Open the
-
Inject dependencies.
- Open the
some-validator-name.validator.ts
file. - Create the
SOME_TOKEN_NAME
instance of theInjectionToken
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.
some-validator-name.validator.ts file/* Import the required functionality from the libraries. */
...
import { InjectionToken } from "@angular/core";
/* The SOME_TOKEN_NAME token that works with Angular DI (dependency injection). */
export const SOME_TOKEN_NAME = new InjectionToken<string>('SOME_TOKEN_NAME');
...
export class SomeValidatorNameValidator extends BaseValidator {
/* Mark DI in the constructor using the @CrtInject decorator. */
constructor(@CrtInject(SOME_TOKEN_NAME) private _name: string) {
super();
}
...
} - Open the
-
Register the
SomeValidatorNameValidator
validator as a validator and theSOME_TOKEN_NAME
token.-
Open the
app.module.ts
file. -
Add the
SomeValidatorNameValidator
validator to thevalidators
section in theCrtModule
decorator. -
Add the configuration object to the
providers
section in theNgModule
decorator.- Set the
provide
property toSOME_TOKEN_NAME
. - Set the
useValue
property to the validation value.
- Set the
-
Implement the
resolveDependency()
method in thebootstrapCrtModule()
method of theAppModule
root module. ThebootstrapCrtModule()
method registers theSomeValidatorNameValidator
validator flagged using theCrtModule
decorator. TheresolveDependency()
method receives the dependencies of the SomeValidatorNameValidator converter. -
Import the required functionality from the libraries into the class.
-
Save the file.
app.module.ts file/* Import the required functionality from the libraries. */
import { DoBootstrap, Injector, NgModule, ProviderToken } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { bootstrapCrtModule, CrtModule } from '@creatio-devkit/common';
import { SomeValidatorNameValidator, SOME_TOKEN_NAME} from "./some-validator-name.validator";
@CrtModule({
validators: [SomeValidatorNameValidator],
})
@NgModule({
declarations: [],
imports: [BrowserModule],
providers: [{
provide: SOME_TOKEN_NAME,
useValue: 'SomeValue'
}],
})
export class AppModule implements DoBootstrap {
constructor(private _injector: Injector) { }
ngDoBootstrap(): void {
/* Bootstrap CrtModule definitions. */
bootstrapCrtModule('some_package_name', AppModule, {
resolveDependency: (token) => this._injector.get(<ProviderToken<unknown>>token)
});
}
} -
-
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 theviewModelConfigDiff
schema section.viewModelConfigDiff schema sectionviewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG*/{
"attributes": {
"SomeField": {
"modelConfigDiff": {
"path": "PDS.SomeField"
},
"validators": {
"usr.SomeValidatorNameValidator": {
"type": "usr.SomeValidatorNameValidator",
"params": {}
}
}
},
...
}
}/**SCHEMA_VIEW_MODEL_CONFIG*/, -
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.
See also
Custom UI component implemented using remote module