I am trying to validate a form submit BUT i want to validate a property ONLY if another property is set to true.
my two properties:
[DisplayName(Translations.Education.IsFeaturette)]
public bool IsFeaturette { get; set; }
[DisplayName(Translations.Education.AddFeaturetteFor)]
[CusomValidatorForFeaturettes(IsFeaturette)]
public string[] Featurettes { get; set; }
custom annotation:
public class CusomValidatorForFeaturettes: ValidationAttribute
{
private readonly bool _IsFeatturette;
public CusomValidatorForFeaturettes(bool isFeatturette): base("NOT OK")
{
_IsFeatturette = isFeatturette;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null && _IsFeatturette )
{
var errorMessage = FormatErrorMessage(validationContext.DisplayName);
return new ValidationResult(errorMessage);
}
return ValidationResult.Success;
}
}
Basiclly if isfeature is true then Featurettes MUST have a value!
Error im getting:
An object reference is required for the non-static field, method, or property 'EducationViewModel.IsFeaturette'
I can not make this property static cuz that would give me problems since this property is set with enityframework and I don't wish to change any of this. How can I accomplish this without making the property static?