1

I am using IValidatableObject/Validator to validate my mvc models. So I have something like this

public class Parent
{
    public Child Child { get; set; }
}

public class Child
{
    [Required]
    public string Name { get; set; }
}

So if I run a validator on parent instance I get something like "Name is required".

What I would like is "Child.Name is required" so it needs to include the property name "Child". Can I do this and if I can, how will I do this?

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Pintac
  • 1,565
  • 3
  • 21
  • 38

1 Answers1

1

This will of course not work out of the box because the Attribute is bound to the property.

But you can simply define your custom error message for the attribute by defining it

public class Child
{
   [Required(ErrorMessageString="Child.Name is required")]
   public string Name { get; set; }
}
MichaC
  • 13,104
  • 2
  • 44
  • 56
  • Hi. Thanks the problem is i need to be able to validate Child on its own as-well. So somehow i need to be able to pass context(yeah i thought validation context will help but i could not get anything to work) – Pintac Nov 19 '13 at 13:43