0

I have followed this article and have a passing test showing custom validation error messages being returned from a resource file when a call to Validator.IsValid(someEntity) fails.

I am trying to translate this to the new unobtrusive client-side validation in MVC3 describe by this article. This also more or less works - well the client-side validation does so I can assume my wiring of NHValidators is good, as you can see the following is output:

<input data-val="true" data-val-required="{NotNullNotEmpty}" id="Username" name="Username" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true">

My problem is that this time the CustomMessageInterpolator has not fired - the resource string has not been translated for this property (it's still in curly braces): {NotNullNotEmpty}.

Both my test and the web are usign the same NHValidator config:

var cfg = new FluentConfiguration();
cfg
.SetMessageInterpolator<CustomMessageInterpolator>()
.SetCustomResourceManager("MyProject.Core.Resources.ValidationMessages", Assembly.Load("MyProject.Core"))
.SetDefaultValidatorMode(ValidatorMode.UseAttribute)
.Register(Assembly.Load("MyProject.Core").ValidationDefinitions())

Just for good measure here's the model snippet:

public class LoginModel
  {
    [NotNullNotEmpty(Message = "{NotNullNotEmpty}")]
    public string Username { get; set; }

Long shot I know but any ideas would be appreciated!

1
  • Did you ever find a definitive solution for this? Commented Apr 29, 2013 at 14:55

2 Answers 2

0

I had no luck with this having posted it around a few forums.

In the end I've resorted to using DataAnnotations instead in my View Models for client-side validation.

Of course this is really hacky and a horrible "solution" as I've now two types of validation attributes in my code. (if wondering, yes I'd rather keep my NH Validation attributes 1. as I'm still generating my schema from them and 2. I like the way NH validates before committing to the db i.e. server-side).

If interested, here's how to use resource files to show the correct labels for your controls.

This code snippet shows how to then also rig up custom error messages:

  [DisplayResource(typeof(LabelResources))]
  public class NewChildTagViewModel : ModuleViewModel
  {
    public int ParentId { get; set; }

    [Required]
    public string Title { get; set; }

    [Range(1, 1000, ErrorMessageResourceType = typeof(ValidationMessages), ErrorMessageResourceName = "Display_Order")]
    [Required]
    public int? DisplayOrder { get; set; }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Can you show your code? I presume you used some sort of custom ModelValidatorProvider?

I've got something working like what you described. It required writing a custom AssociatedValidatorProvider and ModelValidator, along the lines described here

http://weblogs.asp.net/srkirkland/archive/2010/07/20/nhibernate-client-validator-asp-net-mvc-2-model-validation.aspx

but changing the MessageOrDefault method in the article above to use the NHibernate Validator DefaultMessageInterpolator to translate the messages.

It sounds like you got most of the way towards getting something working, and you just needed to use the DefaultMessageInterpolator to translate the messages.

1 Comment

Yeah I was fishing around that area when I emailed you. Had to move on though - at that time having spent quite some time on it and I gave up. A lot to get one's head round to inject a damn resource! I'll have a look at it when I've got fewer deadlines...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.