0

I am creating an asp.net mvc 3 app in Visual Studio 2010. Im trying to use FluentValidation for my Models. I installed the packages (FluentValidation, FluentValidationMVC3) using NuGet. My Models are as follows (simplified for this forum):

public class ActivityLine : AbstractValidator<ActivityLine>
{
    [Key]
    [Required]
    public int ActivityLineId { get; set; }

    public virtual ICollection<TimeModel> TimeModels { get; set; }
}

public class ActivityLineValidator : AbstractValidator<ActivityLine>
{
    public ActivityLineValidator()
    {
        RuleFor(x => x.TimeModels).SetCollectionValidator(new TimeModelValidator());
    }
}


[Validator(typeof(TimeModelValidator))]
public class TimeModel
{
    [Key]
    public int TimeModelId { get; set; }
    [Required]
    public int ActivityLineId { get; set; }
    [Required]
    public int LineNumber { get; set; }
    public int startHours { get; set; }
    public int startMinutes { get; set; }
    public int endHours { get; set; }
    public int endMinutes { get; set; }

    public virtual ActivityLine ActivityLine { get; set; }

    [NotMapped]
    public TimeSpan StartTime
    {
        get { return new TimeSpan(startHours, startMinutes, 0); }
        set { startHours = value.Hours; startMinutes = value.Minutes; }
    }
    [NotMapped]
    public TimeSpan EndTime 
    {
        get { return new TimeSpan(endHours, endMinutes, 0); }
        set { endHours = value.Hours; endMinutes = value.Minutes; } 
    }
}

public class TimeModelValidator : AbstractValidator<TimeModel>
{
    public TimeModelValidator()
    {
        RuleFor(timemodel => timemodel.endHours)
           .GreaterThan(timemodel => timemodel.startHours)
           .WithMessage("Start Time Must be before End Time");
    }
}

In my Controller :

[HttpPost]
public ActionResult Create(ActivityLine model)
{
    if (ModelState.IsValid)
    {
        personnel.ActivityLines.Add(model);
        personnel.SaveChanges();
        return RedirectToAction("Index");
    }
    //Model State Not Valid Redisplay Form
    return View(model);
}

ModelState is always true even when I assign invalid values to and instance of TimeModel. The project then bombs when it hits the personnel.SaveChagnes() line. I get a "No Source Available" error. The detail is below:

Locating source for 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs'. Checksum: MD5 {e3 d4 90 62 70 f7 a6 4e 9e ac 62 71 77 21 64 dd}

The file 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs' does not exist. Looking in script documents for 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs'... Looking in the projects for 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs'. The file was not found in a project. Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'... Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'... Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\atl\'... Looking in directory 'C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\'... The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs. The debugger could not locate the source file 'c:\Projects\FluentValidation\src\FluentValidation\Attributes\ValidatorAttribute.cs'.

I assume something is wrong with my configuration, but I cannot figure out what. Has anyone using Fluent had this issue? What do I need to do to resolve it? My project is located on a network share if that makes a difference.

FrstCBC
  • 87
  • 2
  • 9

1 Answers1

1

Your ActivityLine domain model should not derive from AbstractValidator. It should be decorated with the Validator attribute as you did with your TimeModel:

[Validator(typeof(ActivityLineValidator))]
public class ActivityLine
{
    [Key]
    [Required]
    public int ActivityLineId { get; set; }

    public virtual ICollection<TimeModel> TimeModels { get; set; }
}

public class ActivityLineValidator : AbstractValidator<ActivityLine>
{
    public ActivityLineValidator()
    {
        RuleFor(x => x.TimeModels).SetCollectionValidator(new TimeModelValidator());
    }
}

Also make sure you have the following line in your Application_Start to enable the FluentValidation metadata provider as explained in the documentation:

FluentValidationModelValidatorProvider.Configure();

As far as the bombing on the personnel.SaveChagnes() line is concerned, this has strictly nothing to do neither with ASP.NET MVC nor with FluentValidation.NET. I guess it's the data access technology that you are using that you are having problems with.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the quick response. Geez I can't believe I missed that. I guess I had just been staring at the code for too long. – FrstCBC Jun 20 '12 at 14:06