Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6236355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:50:25+00:00 2026-05-24T10:50:25+00:00

I’m hoping I’m missing something simple here. I’ve configured Fluent Validation for integration with

  • 0

I’m hoping I’m missing something simple here.

I’ve configured Fluent Validation for integration with MVC and it’s been working quite well up until now. I’m now working on a scenario where a user is performing a standard create of what’s called a “service”. A service has hours that have to be defined.

The view model for this Create action is defined as follows:

[Validator(typeof (CreateServiceViewModelValidator))]
public class CreateServiceViewModel
{
    public string Name { get; set; }
    //...Other properties...
    public Collection<CreateServiceHoursViewModel> ServiceHours { get; set; }
}

and CreateServiceHoursViewModel is defined as…

public class CreateServiceHoursViewModel
{
    //...Other properties...
    public DayOfWeek DayOfWeekId { get; set; }
    public DateTimeOffset? OpenTime { get; set; }
    public DateTimeOffset? CloseTime { get; set; }
}

The quick and dirty version of the UI ends up as follows:

enter image description here

The problem:

The fluent validation messages for the collection of hours are not showing the expected error message. They’re displaying the standard error messages from Fluent Validation.

Here are my validators:

public class CreateServiceViewModelValidator : AbstractValidator<CreateServiceViewModel>
{
    public CreateServiceViewModelValidator()
    {
        RuleFor(f => f.Name).NotEmpty()
            .WithMessage("You must enter a name for this service.");

        RuleFor(f => f.Description)
            .NotEmpty().WithMessage("Service must have a description")
            .Length(3, 256).WithMessage("Description must be less than 256 characters.");

        RuleFor(f => f.ServiceHours).SetCollectionValidator(new CreateServiceHoursViewModelValidator());
    }
}

and the HoursValidator

public class CreateServiceHoursViewModelValidator : AbstractValidator<CreateServiceHoursViewModel>
{
    public CreateServiceHoursViewModelValidator()
    {
        DateTimeOffset test;
        DayOfWeek enumTest;

        RuleFor(r => r.DayOfWeekId).Must(byteId => Enum.TryParse(byteId.ToString(), out enumTest)).WithMessage("Not a valid day of week...");

        RuleFor(f => f.OpenTime)
            .NotEmpty().WithMessage("Please specify an opening time...")
            .Must(openTime =>
                  DateTimeOffset.TryParse(openTime.HasValue ? openTime.Value.ToString() : String.Empty, out test))
            .WithMessage("Not a valid time...");

        RuleFor(f => f.CloseTime)
            .NotEmpty().WithMessage("Please specify a closing time...")
            .Must(closeTime =>
                  DateTimeOffset.TryParse(closeTime.HasValue ? closeTime.Value.ToString() : String.Empty, out test))
            .WithMessage("Not a valid time...");
    }
}

and with errors on the hours collection:

enter image description here

When I run the validate method manually in my controller action the correct error messages are returned…

var validator = new CreateServiceViewModelValidator();
var results = validator.Validate(model);

foreach (var result in results.Errors)
{
    Console.WriteLine("Property name: " + result.PropertyName);
    Console.WriteLine("Error: " + result.ErrorMessage);
    Console.WriteLine("");
}

This returns the messages I’d expect.

What am I missing or doing incorrect that the error messages for the hours collection from the fluent validations aren’t being persisted to my view? (The main object validators work as expected)

Any info appreciated!

(I can update with my view if needed. I felt this question was plenty long already. Suffice it to say I have a view that uses an editor template to iterate the collection of service hours.)

@for (int weekCounter = 0; weekCounter <= 6; weekCounter++)
{
    @Html.DisplayFor(model => model.ServiceHours[weekCounter])
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:50:26+00:00Added an answer on May 24, 2026 at 10:50 am

    (cross-posted to http://fluentvalidation.codeplex.com/discussions/267990)

    The error messages you’re seeing aren’t coming from FluentValidation.

    “The value is not valid for CloseTime” is an MVC error message that is generated before FluentValidation has a chance to kick in.

    This is happening because FluentValidation works by validating the entire object once all the properties have been set, but in your case the string “*Enter closing time here” is not a valid DateTime, therefore MVC cannot actually set the property to a valid datetime, and generates an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.