Print Page | Close Window

Verifier<> error messages

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=2093
Printed Date: 10-Jun-2026 at 5:28pm


Topic: Verifier<> error messages
Posted By: BrooksAdair
Subject: Verifier<> error messages
Date 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?



Replies:
Posted By: DenisK
Date 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.


Posted By: BrooksAdair
Date 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?


Posted By: DenisK
Date 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.


Posted By: DenisK
Date 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.


Posted By: bala
Date 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.


Posted By: BrooksAdair
Date Posted: 07-Sep-2010 at 6:36am
Ah, that is what I missed. I can change the message when returning the VerifierResult. Thank you.



Print Page | Close Window