IgnoreNulls vs NotNullValidator negated

Topics: Validation Application Block
Mar 12, 2007 at 11:05 PM
Suppose you have a field where you want to validate that the field is null, or if not null then within a certain range, say 100 to 1000 (i.e. assume datatype is a nullable int in your code)

public int? FieldName { get { return this.fieldName; } set { this.fieldName= value; } }

Obviously you would have a RangeValidator such as this to validate the range

RangeValidator(100, RangeBoundaryType.Inclusive, 1000, RangeBoundaryType.Inclusive)

But to add in the other part of the condition would it be better to use the IgnoreNulls or the NotNullValidator negated

IgnoreNulls
RangeValidator(100, RangeBoundaryType.Inclusive, 1000, RangeBoundaryType.Inclusive)

vs

ValidatorComposition(CompositionType.Or)
NotNullValidator(Negated=true)
RangeValidator(100, RangeBoundaryType.Inclusive, 1000, RangeBoundaryType.Inclusive)

It seems that the IgnoreNulls would be simpler, but as there is not a lot of documenation out there I thought I would see what others think in case I am missing something.
Mar 13, 2007 at 12:10 AM
I just posted something about this on another thread. The two examples you gave are equivalent. We built the IgnoreNulls attribute to get around the limited flexibility for Boolean composition in attributes.

Tom
Mar 13, 2007 at 12:29 AM
That makes sense. Thanks
Apr 13, 2007 at 2:47 PM
Ok, that does work, but if you want to display a more user friendly message, how would you do that? Right now I have.

ValidatorComposition(CompositionType.Or)
NotNullValidator(Negated=true)
RangeValidator(1, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive, MessageTemplate = "GP Limit must be between 0 and 100%.")

and the message I get is "All validators failed for key "GPLimit", where I would rather want the "GP Limit must be between 0 and 100%." message to show up. I have tried a few things and I keep getting the same results.
Apr 13, 2007 at 2:50 PM
Nevermind, I did some MORE reading and found this solution to work...sorry for bringing up an old post:

ValidatorComposition(CompositionType.Or, MessageTemplate="GP Limit must be null or between 0% and 100%.")
NotNullValidator(Negated=true)
RangeValidator(0, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive)
Apr 17, 2007 at 3:20 PM
Edited Apr 17, 2007 at 3:20 PM
I have one more issue with this.

Input data: value "88" from a textbox

And my object is as follows:

private Int32? gPLimit = null;

ValidatorComposition(CompositionType.Or, MessageTemplate="GP Limit must be null or between 0% and 100%.")
NotNullValidator(Negated=true)
RangeValidator(0, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive)
public Int32? GPLimit
{
get { return gPLimit; }
set { gPLimit = value; }
}


Basically I want to save a null value if nothing is entered into the textbox and if the user enters a value I want to validate to make sure it is between 0 and 100. With the code I am using I get an error message (Object must be of type Int32) in the RangeChecker.cs class in the IsInRange(T target) function on this line:

int lowerBoundComparison = this.lowerBound.CompareTo(target);

the value of target is = 88.

I am using the "PropertyProxyValidator" tag as well in my aspx linked to the textbox I want to validate. I also have the ValueConvert event linked to the textbox to make sure there is a proper type entered into the textbox (IE numbers only). Any help would be appreciated. Thanks
Apr 17, 2007 at 5:12 PM
Is the behavior any different if you use the IgnoreNulls attribute?

Tom
Apr 17, 2007 at 5:31 PM
Hi Tom,
I tried with using IgnoreNulls:

ValidatorComposition(CompositionType.Or, MessageTemplate="GP Limit must be null or between 0% and 100%.")
//NotNullValidator(Negated=true)
IgnoreNulls
RangeValidator(0, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive)
IgnoreNulls
public Int32? GPLimit
{
get { return gPLimit; }
set { gPLimit = value; }
}


I got the same error, Object must be of type Int32.
Apr 18, 2007 at 10:28 PM
Hi Shawn -

We have looked into this but we have not been able to reproduce the problem. Would you be able to mail me a simple app which shows the problem (to tomholl at microsoft.com).

thanks!
Tom