jwaliszko / ExpressiveAnnotations

Annotation-based conditional validation library.
MIT License
351 stars 123 forks source link

Dynamic error messages in AssertThat annotations. #123

Closed OliverPrice-FBD closed 8 years ago

OliverPrice-FBD commented 8 years ago

Hi,

I'm working on building a multi-language website with Umbraco, and have utilised ExpressiveAnnotations on a few forms.

Everything is working fine apart from the error messages rendered on the front-end when the assertions fail. Currently it is only possible to output a constant value in the form of a string, e.g.:

[AssertThat(StartDateIsGreaterThanToday(StartDate)", ErrorMessage = "Start date must be greater than today")]

The problem I have is that we need this error message to render in a different language based on the website that the user is browsing. With most of our code this is as simple as referencing the Umbraco Dictionary:

@umbraco.library.GetDictionaryItem("startDateNotGreaterThanTodayErrorMessageText")

I've had a look through the documentation and examples, and searched around to see if there's any way to do this out of the box. Before I spend extra time customising the AssertThat annotation to allow for dynamic error messages I wondered whether you'd come across this issue before and if you know of a workaround that might be simpler than full-blown customisation?

Any help would be much appreciated!

Thanks,

Oliver

jwaliszko commented 8 years ago

Hi, the default way is to use resource objects, by assigning specific properties in attributes.

OliverPrice-FBD commented 8 years ago

I had tried this before but had issues, your suggestion to re-look at using ErrorMessageResourceType = typeof() pointed me in the right direction.

In case anyone else is trying to do the same, the following worked for me.

In the ViewModel:

[AssertThat("AssertionMethod()", ErrorMessageResourceType = typeof(ErrorMessageResources), ErrorMessageResourceName = "OurErrorMessage")]

And in the ErrorMessageResources class:

public static string OurErrorMessage { get { return umbraco.library.GetDictionaryItem("Our error message dictionary item name"); } }