not null validator not working

Topics: Validation Application Block
Jan 29, 2009 at 2:02 AM
Edited Jan 29, 2009 at 2:18 AM
Spent the last 3 hours trying to figure out why the NotNullValidator is not working. I am using Ent Lib 4.1. When I replace the NotNullValidator with a StringLengthValidator it works fine. Does anybody have any ideas what is going on? Here is the code not working with a NotNullValidator

[

NotNullValidator(MessageTemplateResourceName = "NotNullValidator", MessageTemplateResourceType = typeof(Resource.Errormessage))]
public string CategoryName
{
get { return categoryName; }
set { categoryName = value; }
}

Here is the code working with a StringLenghtValidator

[StringLengthValidator(1, RangeBoundaryType.Inclusive, 64, RangeBoundaryType.Inclusive, MessageTemplateResourceName = "StringLengthValidator", MessageTemplateResourceType = typeof(Resource.Errormessage))]
public string CategoryName
{
get { return categoryName; }
set { categoryName = value; }
}

 

 

 

 

 

 

 

 

 

 

Jan 29, 2009 at 2:13 AM
I don't see anything wrong with it.  Maybe you could post your validation code. 


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 29, 2009 at 2:21 AM
Hello Sarah, you are right there is nothing wrong with the code, that is what is so frustrating about it. It is as if the NotNullValidator is not firing. I have other validators in the Category class and they all work, only the NotNullValidator is not firing. What gives?

public

 

int Insert(Model.CategoryInfo category)

 

{

 

int categoryID = 0;

 

 

Validator<Model.CategoryInfo> validator = ValidationFactory.CreateValidatorFromAttributes<Model.CategoryInfo>();

 

 

 

ValidationResults results = validator.Validate(category);

 

 

if (results.IsValid)

 

{

 

if (category != null)

 

{

categoryID = dataAccessLayer.Insert(category);

 

if (cachable)

 

{

 

if (categoryID > 0)

 

{

 

categoryCache.RemoveItemsFromCache(CATEGORY_KEY);

 

}

}

}

}

 

return categoryID;

 

}

Jan 29, 2009 at 8:25 AM

Did you try to debug your program? Maybe the categoryname is not actually null but an empty string.

 

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

Jan 29, 2009 at 5:12 PM
Yes, you are right, it is an empty string because the input comes from a textbox on a aspx page. So I guess I have to check for an empty string rather than null. In that case do I have to use the OnValueConvert event to accomplish this?
Jan 30, 2009 at 3:22 AM
Hi,

You can stick to you StringLengthValidator to check for empty string.

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