Could I use CompositionType.And and CompositionType.Or same time?

Apr 8, 2009 at 7:59 AM
         Recently I get a problem:
         I need to validata one attribute email,it's can be string.empty,but if it's not empty it must length5-20 and be email format.
         How can I do it?
Apr 8, 2009 at 8:09 AM
Place a StringLengthValidator and NotNullValidator(negated) within an OrCompositeValidator.  If the value of the email is represented in a property of a class, have the get accessor of that property return null if it's value is null or empty.  


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 8, 2009 at 10:12 AM
Thanks for you replay,but could you give some code to an example?
Apr 8, 2009 at 10:38 AM
public string Email
{
    get
    {
        if(string.IsNullOrEmtpy(email))
        {
            return null;
        }
        return email;
    }
    set
    {
            email = value;
    }
}

Your config would look like:

<ruleset name="Rule Set">

 

<

properties>

 

<

property name="Email">

 

<

validator messageTemplate="" messageTemplateResourceName=""

 

 

messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

name="Or Composite Validator">

 

<

validator negated="true" messageTemplate="" messageTemplateResourceName=""

 

 

messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

name="Not Null Validator" />

 

<

validator lowerBound="5" lowerBoundType="Inclusive" upperBound="20"

 

 

upperBoundType="Inclusive" negated="false" messageTemplate=""

 

 

messageTemplateResourceName="" messageTemplateResourceType=""

 

 

tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

name="String Length Validator" />

 

</

validator>

 

</

property>

 

</

properties>

</
ruleset>

 


Hope this helps.

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Apr 8, 2009 at 11:06 AM
Thank you very much!But Could I write it not use webconfig?
    [StringLengthValidator(0, Ruleset = "RuleSetA")]
    [ValidatorComposition(CompositionType.Or, Ruleset = "RuleSetA")]           --------------I use or First.
    [StringLengthValidator(10, 20, MessageTemplate = "Email地址必需为10至20个字符之间", Ruleset = "RuleSetA")]
    [ValidatorComposition(CompositionType.And, Ruleset = "RuleSetA")]                     --------------------There is The problem!
    [RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Email地址格式错误", Ruleset = "RuleSetA")]
    public string Email
    {
        get { return email; }
        set { email = value; }
    }
Apr 8, 2009 at 11:29 AM
Why are you trying to add another validatorcomposition?  Based on what you want to do, it should be something like this:

[ValidatorComposition(CompositionType.Or)]
[
StringLengthValidator(10, MessageTemplate = "invalid length")]
[
NotNullValidator(Negated = true)]
public string Email
{
    
get;
    
set;
}

 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com

 

 

Apr 9, 2009 at 2:34 AM
Maybe I did not describe my question clearly,I will use another way to explain it.

Regex=\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*;
if(email !=string.empty)
{
    if(email.length<20&&Regex.test(email))
    {
        return "";
    }
    else 
    {
        return "Maybe too long or format is not correct!";
    }
}
else
{
    return "";
}

And not only this question,many times we need to validata an attribute can not only use CompositionType.Or or CompositionType.And can realize.They can't assembled?
Apr 10, 2009 at 9:04 AM
Maybe I did not describe my question clearly,I will use another way to explain it.

Regex=\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*;
if(email !=string.empty)
{
    if(email.length<20&&Regex.test(email))
    {
        return "";
    }
    else 
    {
        return "Maybe too long or format is not correct!";
    }
}
else
{
    return "";
}

And not only this question,many times we need to validata an attribute can not only use CompositionType.Or or CompositionType.And can realize.They can't assembled?  

Could you give me some help about it,thank you very much.
Apr 13, 2009 at 8:07 AM
Edited Apr 13, 2009 at 8:08 AM
Hi,

I'm trying to use CompositionType.And and CompositionType.Or using attribute but i cant seem to make it work. So, i tried using the config. And i manage to make it work somehow. Would you mind trying my solution if this will address your problem. This is the same as what Sarah has suggested I just added something in the config.

public string Email
{
    get
    {
        if(string.IsNullOrEmtpy(email))
        {
            return null;
        }
        return email;
    }
    set
    {
            email = value;
    }
}

<validation>

    <type assemblyName="ValidatingOrComposite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

      name="ValidatingOrComposite.Tao">

      <ruleset name="Rule Set">

        <properties>

          <property name="Email">

            <validator messageTemplate="" messageTemplateResourceName=""

              messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.OrCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

              name="Or Composite Validator">

              <validator negated="true" messageTemplate="&quot;&quot;" messageTemplateResourceName=""

                messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                name="Not Null Validator" />

              <validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                name="And Composite Validator">

                <validator lowerBound="5" lowerBoundType="Ignore" upperBound="20"

                  upperBoundType="Ignore" negated="false" messageTemplate="5 to 20."

                  messageTemplateResourceName="" messageTemplateResourceType=""

                  tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                  name="String Length Validator" />

                <validator pattern="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

                  options="None" patternResourceName="" patternResourceType=""

                  messageTemplate="Must be valid email." messageTemplateResourceName=""

                  messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

                  name="Regex Validator" />

              </validator>

            </validator>

          </property>

        </properties>

      </ruleset>

    </type>

  </validation>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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