New Posts New Posts RSS Feed: StringLengthVerifier no longer throw exception for empty string
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

StringLengthVerifier no longer throw exception for empty string

 Post Reply Post Reply
Author
afrizal.chen View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Dec-2008
Location: Singapore
Posts: 21
Post Options Post Options   Quote afrizal.chen Quote  Post ReplyReply Direct Link To This Post Topic: StringLengthVerifier no longer throw exception for empty string
    Posted: 11-Jun-2012 at 8:59pm
Thanks DenisK,
it's clear to me now.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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")));
    }

Back to Top
afrizal.chen View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Dec-2008
Location: Singapore
Posts: 21
Post Options Post Options   Quote afrizal.chen Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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();
    }
  }
Back to Top
afrizal.chen View Drop Down
Newbie
Newbie
Avatar

Joined: 17-Dec-2008
Location: Singapore
Posts: 21
Post Options Post Options   Quote afrizal.chen Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down