I'm using Simon Ince's RequiredIf custom validation attribute implementation. For most situations it has been working, however, I have come into a situation that doesn't work and I can't seem to figure out why.
Here is the relevant portion of the model:
public class SiteOptionsViewModel
{
public short? RetrievalVendorID { get; set; }
public short? CopyServiceID { get; set; }
[Required(ErrorMessage = "Select Retrieval Method For Site")]
public short? RetrievalMethodID { get; set; }
//drop down list items
[DisplayName(@"Retrieval Vendor")]
public IEnumerable<SelectListItem> RetrievalVendors { get; set; }
[RequiredIf("RetrievalMethodID", 10, ErrorMessage = @"Copy Service required if Retrieval Method = OS")]
[DisplayName(@"Copy Service")]
public IEnumerable<SelectListItem> CopyServices { get; set; }
[DisplayName(@"Record Format")]
public IEnumerable<ExtendedSelectListItem> RetrievalMethods { get; set; }
}
In the RequiredIfAttribute class, we have to override the IsValid() method:
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// check if the current value matches the target value
if (ShouldRunValidation(value, this.DependentProperty, this.TargetValue, validationContext))
{
// match => means we should try validating this field
if (!_innerAttribute.IsValid(value))
// validation failed - return an error
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
You'll notice that the two properties in the model that are relevant are both IEnumerable<SelectedListItem>
types for use by dropdowns. The requirement is that when a user selects a particular value from the Retrieval Method dropdown, the form must now require a value to be selected from the Copy Service dropdown. The problem I'm having is that when IsValid()
is called on the RequiredIf
attribute, the value parameter is null, even when a value has been selected in the Copy Service dropdown. So, the Copy Service gets flagged as being required even though a value has been selected. Any suggestions on how to fix this?