If you check RequiredAttribute
in reference source, you will see overriden IsValid
method like this:
public override bool IsValid(object value)
{
// checks if the object has null value
if (value == null)
{
return false;
}
// other stuff
return true;
}
The problem here is IsValid
method only checks for null values & null objects, but does not check Count
property which exists in collection objects e.g. IEnumerable<T>
. If you want to check against zero-valued Count
property (which indicates no selected items), you need to create custom annotation attribute inherited from RequiredAttribute
containing IEnumerator.MoveNext()
check and apply it to List<T>
property:
[AttributeUsage(AttributeTargets.Property)]
public sealed class RequiredListAttribute : RequiredAttribute
{
public override bool IsValid(object value)
{
var list = value as IEnumerable;
// check against both null and available items inside the list
return list != null && list.GetEnumerator().MoveNext();
}
}
// Viewmodel implementation
public class ViewModel
{
[RequiredList(ErrorMessage = "Please select a member")]
public List<int> Members { get; set; }
}
Note:
Using int[]
array type instead of List<int>
e.g. public int[] Members { get; set; }
should work for standard RequiredAttribute
because array property returns null
when no items are selected, while List<T>
properties call default constructor which will create an empty list.
Related issue:
Required Attribute on Generic List Property