Tutorial - Logistics - Validators

DocDigitizer WorldObjects Tutorial - create validators

We will continuing using previous tutorial "Logistics" and add validator to show how to use this feature

Add Validation to TransportGuide

Open your Visual Code and login.
Open object TransportGuide.

Add to the existing property documentNumber the following validation rule:

  • Document Number must be filled

Find property documentNumber and bellow the property and the following code lines

Validation 
    return !_value ? "Value must be filled" : null;
End Validation
  • _value reference the property in the scope.

If you return null, the validation engine will assume True.

Code above is the some as doing:

Validation 
    return _validate.NotEmpty(_value);
End Validation

The result of the validation will be structured:

  • IsValid Boolean
  • ErrorMessage error validation message if IsValid is False

Also you can have global Validator that is associate with the Object (not with a Property).

Lets add global validation of the Object TransportGuide, where transport guide have to have at least one shipping guide.

Validation
    return _this.shippingGuides.lengh == 0 ? "Must have at least one shipping guide" : null;
End Validation

The full code of the TransportGuide have to be:

Object TransportGuide
Version 1.0.0

Property documentNumber type Text    description "Document Number"
    Validation 
        return !_value ? "Value must be filled" : null;
    End Validation
Property transporterName    type Text    description "Transporter Name"
Property destinationAddress    type Text    description "Destination Address"
Reference destinationCountry    to Global.Country   
Property deliverDate    type Date    description "Deliver Date"

Collection shippingGuides of ShippingGuide objects

Primary Indexer documentNumber //Property documentNumber must have unique values

Validation
    return _this.shippingGuides.length > 0 ? null : "Must have at least one shipping guide";
End Validation

Download & Update Nuget packages

Build and download Nuget packages.
On build file ("Logistics") select "Download" (choosing directory where you want to download the files)

Open your Visual Studio and the project previous created "Tutorial - Logistics".

Update the 2 Nuget packages on your Visual Studio project.
note: before updating, on command prompt (cmd), PowerShell or Package Manager Console of Visual Studio execute the command bellow

dotnet nuget locals all --clear

This clean nuget cache of the system, ensuring that version want is running

Use of Validaters

On class "Program.cs" add the following function:

static void ValidateTransportGuide()
{
  transport.documentNumber = "";
  Console.WriteLine("");
  Console.WriteLine($"Transport Guide Validation for \"documentNumber\"  : " + $"{transport.documentNumber_Validate()}");
  transport.documentNumber = "transpor1";
  Console.WriteLine("");
  Console.WriteLine($"Transport Guide Validation for \"documentNumber\"  : " + $"{transport.documentNumber_Validate()}");
  Console.WriteLine("");

  Console.WriteLine($"Transport Guide Validation for \"shippingGuides\"  : " + $"{transport.Validate()}");
  Console.WriteLine("");
  transport.shippingGuides.Clear();
  Console.WriteLine($"Transport Guide Validation for \"shippingGuides\"  : " + $"{transport.Validate()}");
  Console.WriteLine("");
}

Add call on "main" to the function that you have create.

static void Main(string[] args)
{
  //ShowCatalogCurrency();
  //ShowCatalogCountry();
  CreateTransportGuide();
  ValidateTransportGuide();
}

Run the program and in the command prompt you should see the messages of the validations performed.

The solution is available:
Source


Did this page help you?