Skip to content

ConditionAttribute

Navid Mehralizadeh edited this page Sep 28, 2019 · 1 revision

This attribute has two property that explains the below:

  • FactArgs : List of string
  • ExceptionMessage : string

All property for this attribute is required. FactArgs accept comma-separated values that we split and store a list of string. ExceptionMessage is a string and we will pass when a condition return false.

Condition methods can return bool values and you must define args related to FactArgs with the same priority.

Example

[Condition("Birthdate,Gender", "If you are boy Your age must be more than 18 years old else more than 20 years old.")]
public bool CheckAgeByGender(DateTime BirthDate, bool Gender)
{
        return Gender ? CalcualteAge(BirthDate) > 18 : CalcualteAge(BirthDate) > 20;
}

private int CalcualteAge(DateTime BirthDate)
{
        var today = DateTime.Today;
        var age = today.Year - BirthDate.Year;
        if (BirthDate.Date > today.AddYears(-age)) age--;
        return age;
}

[Condition("NationalId", "Your NationalId is not valid.")]
public bool NationalIdIsValid(string NationalId)
{
        return NationalId.Length == 10;
}
Clone this wiki locally