New Posts New Posts RSS Feed: Verifier<> error messages
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Verifier<> error messages

 Post Reply Post Reply
Author
BrooksAdair View Drop Down
Newbie
Newbie


Joined: 08-Oct-2008
Location: South Florida
Posts: 14
Post Options Post Options   Quote BrooksAdair Quote  Post ReplyReply Direct Link To This Post Topic: Verifier<> error messages
    Posted: 25-Aug-2010 at 8:20am
I have a number of verifiers that are subclasses of Verifier<>. In DevForce Classic I had overridden the Description property to create a dynamic error message based on the state of the item being verified. I have not been able to figure out how to do this in DevForce EF 2010. The verifiers ErrorMessageInfo property does not seem to be able to changed after the verifier has been added to the engine. All the properties on ErrorMessageInfo seem to be read only. How can this be done?
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: 30-Aug-2010 at 12:50pm
Brooks;

We actually have a new feature in DevForce 2010 that allows you to directly assign an error message to ErrorMessageInfo.ErrorMessage property. Please see code sample below.

private static Verifier GetBirthDateRangeVerifier() {
      Verifier v = new DateTimeRangeVerifier(
          typeof(Employee),       // Type of the object being verified
          Employee.PropertyMetadata.BirthDate.Name, // Property trigger
          false,                  // Non-null value is not required
          MinBirthDate, true,      // starting min date (inclusive)
          DateTime.Today.Subtract(new TimeSpan(16 * 365, 0, 0, 0, 0)), true);    // (inclusive)

      v.VerifierArgs.ErrorMessageInfo.ErrorMessage =
        "The BirthDate must fall in the 20th century or later; " +
        "and Employee must be at least 16 years of age today.";

      return v;
    }

I hope this helps.
Back to Top
BrooksAdair View Drop Down
Newbie
Newbie


Joined: 08-Oct-2008
Location: South Florida
Posts: 14
Post Options Post Options   Quote BrooksAdair Quote  Post ReplyReply Direct Link To This Post Posted: 31-Aug-2010 at 2:42pm
I use that method when I have static messages. My problem is that I have a dynamic message that changes based on the state of the Entity being validated. I have found that any attempt to change a message after it had been added to the verification engine fails.

Anything I can do for dynamic messages?
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: 01-Sep-2010 at 11:42am
Brooks;

Yes, all verifiers are immutable after being added to the verification engine. 

One solution that I can think of is to create a DelegateVerifier of your own and put some logic in the VerifierCondition delegate that changes the error message depending on the Entity state.

private static Verifier GetBornBeforeHiredVerifier() {
      string errorMessage = "Must be born before hired.";
      string[] propertyNames = {Employee.PropertyMetadata.BirthDate.Name, 
                                         Employee.PropertyMetadata.HireDate.Name};

      DelegateVerifier<Employee> v = new DelegateVerifier<Employee>(errorMessage, BornBeforeHiredCondition);
      v.AddTriggers(Employee.PropertyMetadata.BirthDate.Name, Employee.PropertyMetadata.HireDate.Name);
      v.VerifierOptions.ExecutionModes = VerifierExecutionModes.InstanceAndOnAfterSetTriggers;

      return v;
    }

    private static VerifierResult BornBeforeHiredCondition(
        Employee pEmp, TriggerContext pTriggerContext, VerifierContext pVerifierContext) {

      if (pTriggerContext != null &&
          pTriggerContext.Timing == TriggerTiming.BeforeSet) {
        throw new VerifierException("BornBeforeHired verifier not implemented for Preset");
      }

      string errorMessage;
      switch (pEmp.EntityAspect.EntityState) {
        case EntityState.Added:
          errorMessage = EntityState.Added.ToString();
          break;
        case EntityState.AllButDetached:
          errorMessage = EntityState.AllButDetached.ToString();
          break;
        case EntityState.AnyAddedModifiedOrDeleted:
          errorMessage = EntityState.AnyAddedModifiedOrDeleted.ToString();
          break;
        default:
          errorMessage = "Default error message";
          break;
      }
      return new VerifierResult(pEmp.BirthDate < pEmp.HireDate, errorMessage);
    }

I hope this helps.
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: 01-Sep-2010 at 11:44am
Additional notes:

When you execute an instance verification with VerifierEngine.execute(anEmployee), it returns a VerifierResultCollection that has a list of all the VerifierResult. You'll be able to grab the error message that comes back from a particular VerifierResult.
Back to Top
bala View Drop Down
Groupie
Groupie
Avatar

Joined: 18-Aug-2010
Location: Brazil
Posts: 54
Post Options Post Options   Quote bala Quote  Post ReplyReply Direct Link To This Post Posted: 06-Sep-2010 at 4:42am
How we can Bind When We have to do validation with text Box
 
I write code but need to validate  before entering data  I am able to fire
validation.
Back to Top
BrooksAdair View Drop Down
Newbie
Newbie


Joined: 08-Oct-2008
Location: South Florida
Posts: 14
Post Options Post Options   Quote BrooksAdair Quote  Post ReplyReply Direct Link To This Post Posted: 07-Sep-2010 at 6:36am
Ah, that is what I missed. I can change the message when returning the VerifierResult. Thank you.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down