Fix for Patch 4179 [Client side validation of child properties (validate n levels in model type hierachy tree)]

Developer
Oct 20, 2009 at 9:52 AM

About patch 4181 I did not think about using two or more controls which was the easy and fastest way to go. Thanks John!!!

But for this to work I had to make a change in DataAnnotationsValidationRunner.cs so any property that is non primitive or class must be ignored as in the following case:

 

public class Booking
{
[Required]
[StringLength(15)]
public string ClientName { get; set; }

[Required]
[Range(1, 20)]
public int NumberOfGuests { get; set; }

[Required]
public TripDates Dates { get; set; }
}

public class TripDates
{
[Required]
[DataType(DataType.Date)]
public DateTime From { get; set; }

[Required]
[DataType(DataType.Date)]
public DateTime To { get; set; }
}

 

I have one control to validate Booking class and one to validate TripDates. The change makes DataAnnotationsValidationRunner ignore the Dates property of Booking class cause:

  1. There is no way the control will be able to contruct the instance of TripDates to validate it
  2. I´m using a second control to validate Tripdates.

Let me know if this is important to you so I can submit the patch.

Carlos Mendible

 

 

Coordinator
Oct 20, 2009 at 3:28 PM

My initial thought would be to remove the [Required] attribute on Booking.TripDates. I wonder how xVal handles complex property types ... I'll have to take a look.

Developer
Oct 20, 2009 at 3:35 PM

Removing the Requiered attribute from Dates properties would let my custom validation framework (NHibernate.Validator + DataAnnotations + CSLA ) accept null values for that property in Booking class which is not correct.

I´ll look at XVal too and let you know.

Carlos.

Coordinator
Oct 20, 2009 at 3:47 PM

I thought you might say that :). Here's something that might help: http://stackoverflow.com/questions/1369797/xval-how-do-i-validate-child-properties-of-complex-types.

Rob Sutherland's answer looks pretty close, but I would use prop.Attributes.OfType<ValidationAttribute>() (like DataAnnotationsValidationRunner) instead of a marker interface. That would fix DataAnnotationsValidationRunner.GetErrors(), but not the client validation rules. I'll keep looking as I have time.

Thanks!

Coordinator
Oct 23, 2009 at 1:11 PM

I created Work Item for this: http://xvalwebforms.codeplex.com/WorkItem/View.aspx?WorkItemId=4552