Validator Application Block

S

Scott Stark

I'm using the Microsoft Validator Application Block with attributes to
assign validators to various fields in a class. It's pretty straightforward:

// public fields used for brevity

[StringLengthValidator(1,50)]
public string someValue;

[StringLengthValidator(1,50)]
public string someOtherValue;

[???????]
public string yetAnotherValue;

My question is, how would I assign a validator to "yetAnotherValue" ONLY IF
"someOtherValue" is not null? Is there a conditional operator that can point
to another field?

Validating individual fields is simple enough, I just don't know how to do a
conditional validation.

Thanks.
 
S

Scott Stark

Found the answer for anyone who might be able to use it. The SelfValidation
attribute allows for custom validation logic.

[HasSelfValidation]
public class Customer
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}

[SelfValidation]
public void Validate(ValidationResults results)
{
if (string.IsNullOrEmpty(_name))
{
results.AddResult(
new ValidationResult(
"Name cannot be null.",
this,
"Name",
null,
null
)
);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top