Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change attribute error message

I am overriding the RequiredAttribute and in my IsValid method I need the ability to change the ErrorMessage property. Is this possible?

like image 827
user517406 Avatar asked Jul 23 '26 14:07

user517406


1 Answers

[Required(ErrorMessage = "Some custom required message about the Foo property")]
public string Foo { get; set; }

and inside the IsValid method:

public class MyRequiredAttribute : RequiredAttribute
{
    public override bool IsValid(object value)
    {
        ErrorMessage = "Some custom required message";
        ...
        return base.IsValid(value);
    }
}
like image 179
Darin Dimitrov Avatar answered Jul 25 '26 18:07

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!