0

I have class:

public class MultilanguageClass{
   public string En { get; set; }
   public string De { get; set; }
}

And now I want to create class:

public class MyShortText{
   [MaxLength(40, ErrorMessage = "Name cannot be longer than 40 characters.")]
   public MultilanguageClass Name { get; set; }
}

public class MyLongText{
   [MaxLength(140, ErrorMessage = "Name cannot be longer than 40 characters.")]
   public MultilanguageClass Name { get; set; }
}

Now I want, this 40 characters will be actual for En and De in my MyShortText class and 140 for MyLongText class. How to implement this?

cadi2108
  • 1,280
  • 6
  • 19
  • 43
  • You'll need to write your own custom attribute which checks each field. It will derive from `System.ComponentModel.DataAnnotations.ValidationAttribute`. See https://msdn.microsoft.com/en-us/library/cc668224.aspx – Andy Nichols Jan 18 '16 at 16:35
  • 1
    See also http://stackoverflow.com/questions/11959431/how-to-create-a-custom-validation-attribute?rq=1 which is clearer than the msdn article. – Andy Nichols Jan 18 '16 at 16:38
  • Thank you very much, I read about ValidationAttribute, but I had no idea how start this. Now I know :) – cadi2108 Jan 18 '16 at 22:06
  • You cannot get client side validation if you apply a validation attribute to a class (as opposed to a property) and creating a validation attribute for server side validation will involve significantly more code than simply creating 2 class (say) `ShortMultilanguageClass` and `LongMultilanguageClass` where you apply the relevant `[MaxLength]` attribute to each property in the class (and the technique used in the linked answer is not relevant to your issue) –  Jan 18 '16 at 23:08

0 Answers0