0

Right now, it is just displaying a generic

'Initials' should not be empty.

message for the following rule:

context.RulesFor(p => p.Initials).Required(p => p.Message("Initials are frikking required.")).Length(0, 8);

The initials propety is declared in a rather large model of type ApplicantProfileModel : MappedViewModel<ApplicantProfile> as just plain public string Initials { get; set; }, without even a display name attribute, yet I have the same problem even when I add a display name.

I have no Required attributes left in the view models.

The controller action code is:

[HttpGet]
public ActionResult Create()
{
    var applicant = _applicantService.BuildApplicantProfile();
    var model = new ApplicantProfileModel();
    model.MapFromEntity(applicant);
    return View(model);
}

This is my standard code for my last umpteen projects, and everything works except the FluentValidation custom messages. Their validations wok fine.

The view is:

<li class="form-line">
    @Html.LabelFor(m => m.Initials)
    @Html.EditorFor(m => m.Initials)
    @Html.ValidationMessageFor(m => m.Initials)
</li>

I tried TextBoxFor as well, but no difference.

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Your code seems fine. What's the type of the `Initials` property? Could you show a full example (including model, view and controller) allowing to reproduce the problem you are describing here? – Darin Dimitrov Jan 21 '13 at 07:34

2 Answers2

0

Have you left a [Required] DataAnnotation attribute on the property? I've found that those get displayed instead of my custom validation rules if they've been left on.

EDIT: This is how I've set the validators up in the past - it may also be that they're not firing because you're doing a HttpGet rather than a HttpPost and you need to manually call Validate?

public class ApplicantProfileModelValidator : AbstractValidator<ApplicantProfileModel>
{
    public ApplicantProfileModelValidator()
    {
        RuleFor(r => r.Initials)
            .NotEmpty()
            .WithMessage("Initials are frikking required.");
    }
}
levelnis
  • 7,665
  • 6
  • 37
  • 61
0

Turns out I had mixed up two different, but very similar valiation libraries. I initially started with ASP.NET MVC fluent validation framework, or known on NuGet as FluentMVC. This requires your model to implement a method called InitializeModel(ModelContext<TModel> context), in which you add rules for various properties, e.g.

context.RulesFor(p => p.Id).Required(p => p.Message("Id is missing. Message may have been tampered with.").StopOnFail());

The validation engine somehow calls this method on all models, because although instructed to implement an IModelInitializer<TModel> interface on your validated models, I am not. This is not to say the rules are being registered, but some part of this library is active and interfeing with the library I am trying to use, FluentValidation, known on NuGet as FluentValidation, FluentValidation.MVC3 and FluentValidation.MVC4.

Having rmoved the InitializeModel mehod from my model, things seem to be working. My rude test messages for validation now show, so they won't sneak past testing and the release manager.

ProfK
  • 49,207
  • 121
  • 399
  • 775