I just reflected over the code and found that it was using some reflection to 
retrieve the custom 
attributes. So I added in this code to see if the attributes were, in fact, 
visible.

                foreach (PropertyInfo info in typeof(Term).GetProperties())
                {
                    foreach (Attribute attribute in 
info.GetCustomAttributes(true).Cast<Attribute>())
                    {
                        Debug.WriteLine(attribute);
                    }
                }

And the answer is that it can see all the normal attributes, such as 
RequiredAttribute, 
DisplayAttribute, etc, but NOT the CustomValidationAttribute. Note that I have 
done a clean before 
building. I don't know why it's not recognising the CustomValidation attribute. 
Perhaps it is 
something to do with the meta data needing to map the particular attribute to 
the original class?

T.

On Thu, Jan 28th, 2010 at 3:32 PM, ton...@tpg.com.au wrote:

> 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: ozsilverlight-boun...@ozsilverlight.com
> [mailto:ozsilverlight-boun...@ozsilverlight.com] 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)
> m...@miguelmadero.com
> 
> _______________________________________________
> ozsilverlight mailing list
> ozsilverlight@ozsilverlight.com
> http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
> 
> 
> 



_______________________________________________
ozsilverlight mailing list
ozsilverlight@ozsilverlight.com
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight

Reply via email to