In it's simplest form, a custom validator overrides the Validate() method to validate a column value.
public class NotBlankTrimmedColumnValidationRule : ColumnValidationRule
{
public NotBlankTrimmedColumnValidationRule() : base() { }
public override bool Validate(object value)
{
if (value != null && value is string)
value = (value as string).Trim();
bool result = ValidationHelper.Validate(value, ConditionOperator.IsNotBlank, value, null, null);
if (!result)
ErrorText = "This value cannot be blank or contain only whitespace.";
return result;
}
}
The value passed in to Validate() is the row cell value of the column to be validated. In this case we are using the Developer Express ValidationHelper to finish our validation. We could have easily coded this part as well.
Within the validator you can get additional information about the row or view including the RowHandle, FocusedColumn, ColumnByFieldName, GetRowCellValue, etc.
The following example gets the value of the 'Age' column and ensures that the 'Code' column ends with a space and then the age.
public class ValidateRow : ColumnValidationRule
{
public ValidateRow() : base() { }
public override bool Validate(object value)
{
ErrorText = String.Empty;
object codeObj = GetRowCellValue("Code");
string code = codeObj.ToString();
object ageObj = GetRowCellValue("Age");
int age = Convert.ToInt32(ageObj);
bool result = code.EndsWith(" " + age.ToString());
if (!result)
{
ErrorText = "'Code' must end with 'Age'";
SetColumnError(ColumnByFieldName("Code"), ErrorText);
}
return result;
}
}