Reputation: 5729
I know that i from Application_Start can ActionFilterAttribute add a custom global filter and manipulate the ModelState and what not.
Is there a similar way, to access the @Html (HtmlHelper) Before it get's send to the view?
The reason for this is that i want to edit (or remove and recreate) the UnobtrosiveValidationAttributes. And if i try to do that in the View like this: @Html.GetUnobtrusiveValidationAttributes("PhoneNumber").Clear();
Nothing happens, but i'm thinking it might would work if i got to it earlier?
(If you are wondering why: i need to translate the ErrorMessages inside)
Upvotes: 1
Views: 70
Reputation: 1166
I'm not sure if there are ways to intercept where the unobtrusive validating code is assigning the message text. I'm not sure that it's the best idea either because one property could have many different validations (Required, Regex, StringLength, etc...)
I can tell you there are other ways to localize error messages though. One way that works out of the box is to use resource files and to define a resource key instead of an error message.
[Required(ErrorMessageResourceName="resource-key")]
public string PhoneNumber { get; set; }
Another way that works but requires writing more code is to create your own custom validators that retrieve your error messages from wherever they are stored. I had to recently do this because all of our localization happens in the database.
Upvotes: 2