Validation on two properties

Topics: Validation Application Block
Aug 1, 2007 at 4:26 AM
Hi
I am not sure whether validation application block supports the feature I want. I have a method receiving a object as a parameter. I want to setup validation on the properties of the object using config file. If the object has properties A and B. I want to make sure atleast on of them is NOT null. How do I do this validation using VAB.

Appreciate your time and help.
Aug 1, 2007 at 1:36 PM
Hi,

This is not possible with the standard validators through the declarative approach (attributes and configuration). Depending on your scenario, you can either build a composite validator through code (doing an "or" of two not null validations on the properties), use self validation or create your own validator.

Regards,
Fernando
Aug 1, 2007 at 3:19 PM
Thanks for your reply Fernando. Do you have an example to do this?

Thanks in advance.


fsimonazzi wrote:
Hi,

This is not possible with the standard validators through the declarative approach (attributes and configuration). Depending on your scenario, you can either build a composite validator through code (doing an "or" of two not null validations on the properties), use self validation or create your own validator.

Regards,
Fernando

Aug 1, 2007 at 7:27 PM
Edited Aug 1, 2007 at 7:37 PM
This is what I finally ended up doing.

The caller is changed like this.

ValidationHandler.ValidateRequest<RegisterChannelRequest>(request, delegate(RegisterChannelRequest match)
{
return (match.ChannelId == null && string.IsNullOrEmpty(match.Url));
},
Resources.RegisterChannelValidationRule,
ValidationMessages.ChannelIdAndUrlNotAvailable);


This is the method I am calling to do custom validation.

36 public static void ValidateRequest<T>(T request, Predicate<T> customValidation, string validationRule, string customValidationErrorMessage)
37 {
38 if (customValidation(request))
39 {
40 throw new ValidationFailedException(customValidationErrorMessage);
41 }
42
43 ValidationResults results = Validation.Validate<T>(request, validationRule);
44
45 if (!results.IsValid)
46 {
47 StringBuilder sbValMessages = new StringBuilder();
48
49 foreach (ValidationResult valResult in results)
50 {
51 sbValMessages.AppendFormat("{0}{1} : {2}", (sbValMessages.Length > 0) ? Environment.NewLine : string.Empty, string.IsNullOrEmpty(valResult.Tag) ? valResult.Key : valResult.Tag, valResult.Message);
52 }
53
54 throw new ValidationFailedException(sbValMessages.ToString());
55
56 }
57
58 return;
59 }


NikkamM wrote:
Thanks for your reply Fernando. Do you have an example to do this?

Thanks in advance.


fsimonazzi wrote:
Hi,

This is not possible with the standard validators through the declarative approach (attributes and configuration). Depending on your scenario, you can either build a composite validator through code (doing an "or" of two not null validations on the properties), use self validation or create your own validator.

Regards,
Fernando