Search code examples
c#devforce

DevForce Validation returning Ok for navigation properties that have not been set


I have been working on validatin for our entities in DevForce and I have managed to get everything I need working aside from validating Navigation Properties.

I have tried placing a RequiredValueVerifier attribute on the property and that does show the validation error on the UI but as soon as I use Manager.VerifierEngine.Execute({entitytovalidate}) the result comes back as Ok.

I know DevForce creates nullos and we can modify what the properties have in said nullos but I would like a way that the VeirifierEngine would return not Ok when we have not updated the value from the nullo.

My current work-around is to have a secondary Int32RangeVerifier on the Id that is used for the FKey but I am not to happy with that as a work-around.

Trying to do this without having to create Verifier Providers just for these properties.

If anyone has a solution to this I would be greatly appreciative if you could share.

Here is a sample of the current work-around:

namespace BearPaw.Models.Main 
{
[MetadataType(typeof(TechnicianNoteMetadata))]
public partial class TechnicianNote  {

    public static TechnicianNote Create(int byUserId, DateTimeZone clientZone, DateTime userUtc)
    {
        var newItem = new TechnicianNote()
        {
            CreatedById = byUserId,
            CreatedDate = userUtc,
            CreatedDateTz = clientZone.Id,
            ModifiedById = byUserId,
            ModifiedDate = userUtc,
            ModifiedDateTz = clientZone.Id
        };
        return newItem;
    }

}

    public class TechnicianNoteMetadata
    {
    [Int32RangeVerifier(ErrorMessage = "Note Category is required", MinValue = 1)]
    public static int NoteCategoryId;

    [RequiredValueVerifier(DisplayName = "Note Category")]
    public static NoteCategory NoteCategory;

    [RequiredValueVerifier(DisplayName = "Note Detail")]
    public static string NoteDetail;

    }
}

Many thanks in advance


Solution

  • You can create a custom verifier to handle navigation property validation, and add it directly to the VerifierEngine with the AddVerifier method if you don't want to use an IVerifierProvider.

    For example:

    public class NullEntityVerifier : PropertyValueVerifier
    {
       public NullEntityVerifier(
           Type entityType,
           string propertyName,
           string displayName = null)
          : base(new PropertyValueVerifierArgs(entityType, propertyName, true, displayName)) { }
    
       public NullEntityVerifier(PropertyValueVerifierArgs verifierArgs)
          : base(verifierArgs) { }
    
       protected override VerifierResult VerifyValue(object itemToVerify, object valueToVerify, TriggerContext triggerContext, VerifierContext verifierContext)
       {
           var entity = valueToVerify as Entity;
           var msg = $"{this.ApplicableType.Name}.{this.DisplayName} is required.";
           return new VerifierResult(entity != null && !entity.EntityAspect.IsNullEntity, msg);
       }
    }
    

    To add to the engine:

    var verifier = new NullEntityVerifier(typeof(TechnicianNote), "NoteCategory");
    _em1.VerifierEngine.AddVerifier(verifier);
    

    If you want to stick with attributed verifiers you can create a custom attribute for your verifier. See the DevForce Resource Center for more information.