VAB NotNull Validator with String Length Validator

Topics: Enterprise Library Core, General discussion, Validation Application Block
Sep 17, 2009 at 1:28 PM

Hi Everyone,

suppose there is class

public class Person
{
private string email;

[NotNullValidator(MessageTemplate ="Email cannot be Null")]
[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Invalid Email ID")]
public string Email
{
get { return email; }
  set { this.email= value; }
}
}


when i validate the person class both the NotNullValidator and RegexValidator will fire at a same time.
What i want is fire NotNullValidator only if i wont pass any value for Email and
if i pass the invalid Email then the RegexValidator should be fired .


How to do that .
Can any one let me know.

Thanks in advance
Sreenivas
Sep 18, 2009 at 3:59 AM

Hi,

In your first scenario: What i want is fire NotNullValidator only if i wont pass any value for Email. If you dont pass any value for email, do you want that the RegEx validator wont fire?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Sep 18, 2009 at 1:29 PM
Hi Valiant
I need to fire only not null validator and not the reged validator to fire,
i tried using both types of composite validator .
thanks
sreenivas


From: [email removed]
To: [email removed]
Date: Thu, 17 Sep 2009 20:59:11 -0700
Subject: Re: VAB NotNull Validator with String Length Validator [entlib:69241]

From: AvanadeSupport
Hi,
In your first scenario: What i want is fire NotNullValidator only if i wont pass any value for Email. If you dont pass any value for email, do you want that the RegEx validator wont fire?
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Read the full discussion online.
To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)
To start a new discussion for this project, email entlib@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com
Sep 22, 2009 at 3:54 AM

Hi,

With the current implementation of the CompositionValidator that the ent lib have. Not firing the second validator if the first one fails is not possible. One possible way that I can think of to achieve your goal is to use the SelfValidation. Here is a sample.

    [HasSelfValidation]
    public class Person
    {
        private string email;
        public string Email
        {
            get { return email; }
            set { this.email = value; }
        }

        [SelfValidation]
        public void ValidateEmail(ValidationResults results)
        {
            NotNullValidator notNullValidator = new NotNullValidator("Email must not be null.");
            notNullValidator.Validate(this.Email, results);

            if (!string.IsNullOrEmpty(this.Email))
            {
                RegexValidator regExValidator = new RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", "Invalid Email ID");
                regExValidator.Validate(this.Email, results);
            }
        }
    }
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.