Get error message when using custom validation attribute
Asked Answered
K

4

11

I'm using the CustomValidationAttribute like this

[CustomValidation(typeof(MyValidator),"Validate",ErrorMessage = "Foo")]

And my validator contains this code

public class MyValidator {
    public static ValidationResult Validate(TestProperty testProperty, ValidationContext validationContext) {
        if (string.IsNullOrEmpty(testProperty.Name)) {
            return new ValidationResult(""); <-- how can I get the error message  from the custom validation attribute? 
        }
        return ValidationResult.Success;
    }
}

So how can I get the error message from the custom validation attribute?

Kimura answered 23/9, 2012 at 13:20 Comment(0)
O
7

There's no reliable way to get the error message from the attribute. Alternatively you could write a custom validation attribute:

[MyValidator(ErrorMessage = "Foo")]
public TestProperty SomeProperty { get; set; }

like this:

public class MyValidatorAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var testProperty = (TestProperty)value;
        if (testProperty == null || string.IsNullOrEmpty(testProperty.Name))
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }
}

In this case the error message will be inferred from the custom validation attribute.

Othelia answered 23/9, 2012 at 13:39 Comment(1)
Yes there is a reliable way. For a better answer to the actual question asked please see my answer below.Phosphatize
P
9

I know this is a little of an old post, but I will provide an better answer to the question.

The asker wants to use the CustomValidationAttribute and pass in an error message using the ErrorMessage property.

If you would like your static method to use the error message that you provided when decorating your property, then you return either:

new ValidationResult(string.Empty) or ValidationResult("") or ValidationResult(null).

The CustomValidationAttribute overrides the FormatErrorMessage of its base class and does a conditional check for string.IsNullOrEmpty.

Phosphatize answered 22/10, 2014 at 14:43 Comment(0)
O
7

There's no reliable way to get the error message from the attribute. Alternatively you could write a custom validation attribute:

[MyValidator(ErrorMessage = "Foo")]
public TestProperty SomeProperty { get; set; }

like this:

public class MyValidatorAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var testProperty = (TestProperty)value;
        if (testProperty == null || string.IsNullOrEmpty(testProperty.Name))
        {
            return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
        }

        return null;
    }
}

In this case the error message will be inferred from the custom validation attribute.

Othelia answered 23/9, 2012 at 13:39 Comment(1)
Yes there is a reliable way. For a better answer to the actual question asked please see my answer below.Phosphatize
F
0

You can look into the following posting to get some ideas on how to do what you want to do (they use JS):

Custom validator error text through javascript?

Hope this helps.

Forland answered 23/9, 2012 at 13:37 Comment(0)
H
0

The only way I have found that works is to validate the model from the post back method using TryValidateObject and if it fails, show the model again - then the error will show up.

    [HttpPost]
    public ActionResult Standard(Standard model)
    {
        var valContext = new ValidationContext(model, null, null);
        var valResults = new List<ValidationResult>();;
        bool b = Validator.TryValidateObject(model, valContext, valResults, true);
        if(!b)
            return View(model);
        ...
Hemato answered 10/10, 2013 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.