Hi Miguel,
I ran the code:
var ctx = new ValidationContext(dfTermItem.CurrentItem, null, null) {
MemberName
= "TermCategoryID" };
var validationResults = new Collection<ValidationResult>();
//validate against a guid that should fail
if (Validator.TryValidateProperty(new
Guid("00000000-0000-0000-0022-000000000002"), ctx,
validationResults))
{
Debug.WriteLine("Success");
}
else
{
Debug.WriteLine("Fail");
}
It always returns "Success" and unfortunately, it didn't fire the Custom
Validator at all.
dfTermItem.CurrentItem is of type "Term"
Term has a property TermCategoryID
In the metadata.cs file, TermCategoryID is declared as follows:
[CustomValidation(typeof
(TermCustomValidation),"EnsureTermCategory",ErrorMessageResourceName
= "TTermCategoryRequiredError", ErrorMessageResourceType = typeof
(ValidationErrorResources))]
[Bindable(true, BindingDirection.TwoWay)]
public Guid TermCategoryID;
TermCustomValidation is a static class:
public static class TermCustomValidation
{
public static ValidationResult EnsureTermCategory(Guid TermCategoryID,
ValidationContext
validationContext)
{
Term currentTerm = validationContext.ObjectInstance as Term;
if (currentTerm != null)
{
if (currentTerm.TermCategoryID == Guid.Empty)
{
return new ValidationResult("Term Type is a required
field.");
}
//fyi, EnumHelper.GetID simply returns a guid from an attribute attached to an
enum value
if
(currentTerm.TermCategoryID==EnumHelper.GetID(TermCategoryList.Vendor))
{
if (currentTerm.VendorID == null || currentTerm.VendorID ==
Guid.Empty)
{
return new ValidationResult("Vendor is required when a
Vendor Term Type is
selected.");
}
}
else if (currentTerm.TermCategoryID ==
EnumHelper.GetID(TermCategoryList.Model))
{
if (currentTerm.ModelID == null || currentTerm.ModelID ==
Guid.Empty)
{
return new ValidationResult("Vendor, Brand and Model
are required when a Model
Term Type is selected.");
}
}
}
return ValidationResult.Success;
}
Note that it never actually debugs into the EnsureTermCategory method.
I would have thought it would have returned "Failed"!
Regards,
Tony
From: [email protected]
[mailto:[email protected]] On
Behalf Of Miguel Madero
Sent: Thursday, 28 January 2010 11:10 AM
To: ozSilverlight
Subject: Re: RIA Domain Services and required dependencies
I've not experienced that issue. Try to explicitly validate the property to see
if that's an issue with
the DataForm or just with how you set up the validation.
var ctx = new ValidationContext(this, null, null) { MemberName
= "NameOfYourPropertyGoesHere" };
var validationResults = new Collection<ValidationResult>();
if (Validator.TryValidateProperty("yourNewValueGoesHere", ctx,
validationResults))
// It was valid
else
// it wasn't valid, check the validationResults for details
Could you also try it with the example on the link I sent?
--
Miguel A. Madero Reyes
www.miguelmadero.com (blog)
[email protected]
_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight