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 p开发者_运维百科roperties...
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:
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:
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])
}
(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.
精彩评论