Good I have a list of objects, each object has a checkbox, if the checkbox is true I want to validate that the Name field and the Last name field have a value.
My model:
[ListHasElements(ErrorMessage : "Debe escribir en los casilleros")]
public List<MyObjeto> ListaMyObjeto { get; set; }
This list contains the following fields:
public class MyObjeto{
public bool IsCheck { get; set; }
public string Nombre { get; set; }
public string Apellido { get; set; }
}
This is my custom class
public class ListHasElements : ValidationAttribute
{
public override bool IsValid(List<PlanillaResult> list)
{
if (list != null)
{
foreach (var item in list)
{
if (item.IsCheck == true)
{
if (string.IsNullOrWhiteSpace(item.Nombre) || string.IsNullOrWhiteSpace(item.Apellido))
{
return false;
}
}
}
}
return list.Any();
}
}
But I get the following error: "no suitable member was found to invalidate".
Maybe I'm missing something simple or I'm doing it wrong, what I need is to validate a list through Data Annotations