ValidationContext

Description

A ValidationContext is an interface that exposes the necessary data for validating a dataCell’s content. It extends ValidationValue.

Properties

Property

Type

Description

columnDefinition

ReadonlyColumnDefinition

The column definition of the dataCell to validate.

format

Formatter

The format used in the cellViewer of the dataCell to validate.

options?

ReadonlyValidationRuleOptionsCollection

An object representing the options of this validation context.

Methods

Method

Parameter

Type

Description

getFieldValue

 

 

This method can be used while validating a specific field, to retrieve the value of another field (edited or not), for specific validation needs.

 

field

string

The requested field.

 

[output]

ValidationValue

A ValidationValue (oldValue and newValue) for the requested field.

Example

In the following example, the validation function receives the ValidationContext. This ValidationContext is used to validate the “birth” column. The ValidationContext.newValue represents the edited value of the “birth” column (the oldValue represents the last valid value, before edit). The ValidationContext.getFieldValue(“death”).newValue will return the value of the “death” column for this dataItem, whether it is edited or not. The return value must be a valid or invalid ValidationResult.

function yearRule(validationContext)

{

  if (validationContext.columnDefinition.field === "birth")

  {

    const newBirth = validationContext.newValue;

    const death = validationContext.getFieldValue("death").newValue;

    if (death - newBirth > 100)

    {

      return { isValid: false, errorMessage: `Can't live more than 100 years.` };

    }

    else if (death < newBirth)

    {

      return { isValid: false, errorMessage: `Can't be dead before being borned.` };

    }

  }



  return { isValid: true };

}

The yearRule function should be set as a ValidationRuleDefinition.validate function. This ValidationRuleDefinition should be set in the DataGridOptions.validationRuleDefinitions as a value. The key of this value should be used as the identifier on each of the columns needing this validationRule on the ColumnDefinition.validationRules property.