1

My problem with MVC validation is that I have a lot of different condition where different message needs to be shown and there isn't enough default attribute type for me to put all those message in. I am aware that I could create custom attribute to solve this problem, but then I realize a lot of the validation I want could be done with simple regex checking, so instead of creating multiple different classes I tried to find a way to have multiple regexp attribute on a property.

After some research I found this SO post which suggest simply to inherit the RegularExpressionAttribute class, which is EXACTLY what I want, but I couldn't do the actual implementation myself as I am fairly new to .NET.

Here's the suggested code:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
public class MultiRegularExpressionAttribute: RegularExpressionAttribute
{
    ...
}

can anyone help me fill the actual content of the class out? I tried to fiddle around and it ran without error, but the isValid method never got visited and I am so very lost.

Community
  • 1
  • 1
Jacky Cheng
  • 1,536
  • 1
  • 10
  • 22
  • What do you mean _have more error message variety_? You can only display one message. –  Sep 27 '15 at 11:45
  • I have a lot of different condition where different message needs to be shown and there isn't enough attribute type for me to put all those message in, that's what i meant. – Jacky Cheng Sep 27 '15 at 11:49
  • You cannot apply `AllowMultiple=true` to a validation attribute if your expecting to use client side validation. –  Sep 27 '15 at 11:53

0 Answers0