9

I have an extended class of RequiredAttribute that doesn't send error messages back. If I check it in the debugger the text is there alright.

public class VierRequired : RequiredAttribute
{
    public VierRequired(string controlName)
    {
        //...
    }

    public string VierErrorMessage
    {
        get { return ErrorMessage; }
        set { ErrorMessage = value; }
    }

    // validate true if there is any data at all in the object
    public override bool IsValid(object value)
    {
        if (value != null && !string.IsNullOrEmpty(value.ToString()))
            return true;

        return false; // base.IsValid(value);
    }
}

I call it like this

[VierRequired("FirstName", VierErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }

And the mvc-view

<%: Html.TextBoxFor(model => model.FirstName, new { @class = "formField textBox" })%>
<%: Html.ValidationMessageFor(model => model.FirstName)%>

It works if I use the normal Required annotation

[Required(ErrorMessage = "Please enter your name")]
public string FirstName { get; set; }

But the custom does not send any error message back

4
  • Well, for one thing, your constructor doesn't have the same name as your class. Was that just a transcription error?
    – Ann L.
    Sep 24, 2012 at 21:48
  • What do you mean by "doesn't send error messages back"? Sep 24, 2012 at 21:49
  • @Benjamin The error messages doesn't seem to be returned to the view. But if I use normal Required annotation they do. Sep 24, 2012 at 21:57
  • 2
    I'm skeptical value.ToString() is actually doing what you think it is. You need value as String instead. Sep 24, 2012 at 22:10

1 Answer 1

31

I also had a problem with client side validation when I created my own derivative of the RequiredAttribute. To fix it you need to register your data annotation like so:

DataAnnotationsModelValidatorProvider.RegisterAdapter(
            typeof(VierRequired),
            typeof(RequiredAttributeAdapter));

Simply call this in your Application_Start() method and client side validation should work as normal.

If your attribute is not working when you are POST-ing your form then this would indicate to me that there is something wrong with the logic in your attribute (check you IsValid method). I am also not sure what you are trying to achieve with your derived data annotation; your logic looks like it is trying to do pretty much what the default attribute does anyway:

Taken from the MSDN documentation:

A validation exception is raised if the property is null, contains an empty string (""), or contains only white-space characters.

2
  • That is only required for client-side validation. Sep 24, 2012 at 22:11
  • Turns out this was correct, my setup is somewhat complex since I run the mvc project inside another project. Sep 27, 2012 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.