Validator Application Block

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott Stark

    #1

    Validator Application Block

    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

    [StringLengthVal idator(1,50)]
    public string someValue;

    [StringLengthVal idator(1,50)]
    public string someOtherValue;

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

    My question is, how would I assign a validator to "yetAnotherValu e" 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.

  • Scott Stark

    #2
    Re: Validator Application Block

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

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

    [SelfValidation]
    public void Validate(Valida tionResults results)
    {
    if (string.IsNullO rEmpty(_name))
    {
    results.AddResu lt(
    new ValidationResul t(
    "Name cannot be null.",
    this,
    "Name",
    null,
    null
    )
    );
    }
    }
    }

    "Scott Stark" <email@scottsta rk.comwrote in message
    news:OO%23MijfI JHA.1308@TK2MSF TNGP02.phx.gbl. ..
    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
    >
    [StringLengthVal idator(1,50)]
    public string someValue;
    >
    [StringLengthVal idator(1,50)]
    public string someOtherValue;
    >
    [???????]
    public string yetAnotherValue ;
    >
    My question is, how would I assign a validator to "yetAnotherValu e" 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.

    Comment

    Working...