IgnoreNulls with Configuration?

Topics: Validation Application Block
Aug 15, 2008 at 4:12 PM
I need to do a conditional validation where if it is null, don't validate, otherwise validate it.  The IgnoreNulls attribute seemed to be the answer but I can't get it to work.

I'm driving the validation via configuration.  Adding the attribute did not seem to stop the validation from occuring when the MiddleName property is null (nothing).  If all other validations pass, this one still fails.  Code looks like so:

 <Microsoft.Practices.EnterpriseLibrary.Validation.Validators.IgnoreNulls()> _
 <Runtime.Serialization.DataMember(Order:=3)> _
 Public Overridable Property MiddleName() As String Implements IPersonName.MiddleName
    ...

Config:
...
<property name="MiddleName">
      <validator lowerBound="1" lowerBoundType="Inclusive" upperBound="9"
      upperBoundType="Inclusive" negated="false" messageTemplate="Subject middle name must be 9 characters or less."
...

I have tried it by defining the ruleset name in the attribute as well and this validation still fails.

Guidance appreciated!
Aug 15, 2008 at 7:57 PM
Hi,

In reality there is no "ignore nulls" validator. The [IgnoreNulls] attribute maps to an OR-composition of a Null validator and the actual validation specified, so if you get a null value it will pass (because the Null validator says it's ok, so the OR succeeds), and if you get a non-null value the actual validation will kick-in. You'll need to duplicate this arrangement of validators in your configuration file.

Hope this helps,
Fernando