Print Page | Close Window

StringLengthVerifier no longer throw exception for empty string

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3482
Printed Date: 13-May-2026 at 4:52am


Topic: StringLengthVerifier no longer throw exception for empty string
Posted By: afrizal.chen
Subject: StringLengthVerifier no longer throw exception for empty string
Date Posted: 06-Jun-2012 at 1:42am
I have many properties with such attribute below:
[IbVal.StringLengthVerifier(MaxValue=150, IsRequired=true, ErrorMessageResourceName="Party_ID")]

When I set Party.ID = string.empty; it's no longer throwing "ID is required" exception.
I also check my EntityManager.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull, the value is true.

With these settings, I would expect to receive an exception when Party.ID is set to string.empty.
And I have many code depending on this feature.



Replies:
Posted By: DenisK
Date Posted: 06-Jun-2012 at 5:33pm
Hi Afrizal,

Where do you expect to see the exception thrown? On the client or server?

Remember that the EntityManager and EntityManager.VerifierEngine are both different instance on the client and server. So make sure you're setting the ShouldTreatEmptyStringAsNull flag on both client and server.

A typical place to set this on server is in your custom SaveInterceptor.

public class SaveInterceptor : EntityServerSaveInterceptor {
      protected override bool ValidateSave() {
      EntityManager.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull = false;
      return base.ValidateSave();
    }
  }


Posted By: afrizal.chen
Date Posted: 06-Jun-2012 at 5:53pm
Hi DenisK,
It's on the client side.
Isn't the default value for ShouldTreatEmptyStringAsNull  is true?
As i do not change its value, it shall stays as true, and I would expect to get the exception when the property is set to string.empty.


Posted By: DenisK
Date Posted: 07-Jun-2012 at 12:48pm
Hi Afrizal,

That is correct. I just realized you're expecting an exception but by default, the ErrorNotificationMode is Notify, which causes notification through the INotifyDataErrorInfo and IDataErrorInfo interfaces for binding purposes.

You can change this mode to Exception if you're expecting it throw an exception. See code sample below.

    [TestMethod]
    public void VerifierEmptyOrNullString() {
      var emp = new Employee();
      _em1.AddEntity(emp);
      _em1.VerifierEngine.DefaultVerifierOptions.ShouldTreatEmptyStringAsNull = true;
      
      //Set VerifierEngine to throw exception
      _em1.VerifierEngine.DefaultVerifierOptions.ErrorNotificationMode = VerifierErrorNotificationMode.ThrowException;
      try {
        emp.FirstName = String.Empty;
      } catch (Exception ex) {
        Assert.IsTrue(ex.Message.Contains("required"));
      }

      //Default is Notify
      _em1.VerifierEngine.DefaultVerifierOptions.ErrorNotificationMode = VerifierErrorNotificationMode.Notify;
      emp.FirstName = String.Empty;
      Assert.IsTrue(emp.EntityAspect.ValidationErrors.Any(vr => vr.Message.Contains("required")));
    }

Learn more about ErrorNotificationMode here.  http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-configure#HErrorNotificationMode - http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-configure#HErrorNotificationMode


Posted By: afrizal.chen
Date Posted: 11-Jun-2012 at 8:59pm
Thanks DenisK,
it's clear to me now.



Print Page | Close Window