Print Page | Close Window

Custom validation

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3418
Printed Date: 02-Jun-2024 at 5:47am


Topic: Custom validation
Posted By: giotis
Subject: Custom validation
Date Posted: 01-May-2012 at 2:22am
Dear friends ,
exploring the ValidationErrorMessageProcessor I noticed that only inform the user about the error field and not the
named entity in ValidationErrorsView and I decided to include this information but I found two things,

1. when we add our own custom validations not updated the {Verifier.ApplicableType}
    which keeps the information of the entity

2. Since fields they have RequiredAttribute why require extra custom validation?

    namespace DomainModel {
        [DataContract(IsReference = true)]
        public class Address : AuditEntityBase, IHasRoot    {
             internal Address()        {   }
             ...
             /// <summary>Gets or sets the StateId. </summary>
             [DataMember]
             [Required]
             public Guid StateId { get; set; }
 
              /// <summary>Gets or sets the State. </summary>
              [DataMember]
              [Required]
              public State State { get; set; }
              ...
              public override void Validate(VerifierResultCollection validationErrors)   {
                     if (State.EntityFacts.EntityAspect.IsNullOrPendingEntity)
                          validationErrors.Add(new VerifierResult(VerifierResultCode.Error, "The State field is required", "State"));
              }
     }

best regards



Replies:
Posted By: mgood
Date Posted: 01-May-2012 at 9:17am
Can you provide some more information on your first issue?
 
On your second point. Custom validation is needed here, because StateId and State will always have a value assigned. If you don't explicitly set StateId it will be initialized with Guid.Empty() and State will be initialized with the null entity not the null value. The RequiredAttribute is a .NET attribute that doesn't understand the null entity. It looks for a null value. So, the two options are doing what I'm doing or put a DevForce Verifier on the State property, which understands the null entity.
 
One of the key features in DevForce is that navigation properties are never null, to avoid NullReferenceExceptions when binding to the UI. You can learn more about this topic here:
 
http://drc.ideablade.com/xwiki/bin/view/Documentation/null-entity - http://drc.ideablade.com/xwiki/bin/view/Documentation/null-entity


Posted By: giotis
Date Posted: 01-May-2012 at 4:11pm
When we add a new StaffingResource and after save without fill appropriate Address fields then raise an exception
if we put a breakpoint in ValidationErrorHandler the validationErrors return a VerifierResult collection
So we see that custom Verifier type is null

                            [DataContract]
                            [DebuggerDisplay("{ResultCode}; {Message}; {Verifier.ApplicableType}")]
                            public class VerifierResult   {

validationErrors = { [0] ...
                                [1] = Error; "The Address1 field is required."; {DomainModel.Address}
                                ...
                                [5] = Error; "The State field is required"; null
                                ...
                               }

namespace Common.Validation{
    public class ValidationErrorHandler : IValidationErrorNotification    {
        public void OnValidationError(VerifierResultCollection validationErrors)        {
            if (validationErrors.HasErrors)
                EventFns.Publish(new ValidationErrorMessage(validationErrors));
        }
    }
}


Posted By: giotis
Date Posted: 01-May-2012 at 4:19pm
How to inform the user about the name of entity of field


Posted By: mgood
Date Posted: 01-May-2012 at 4:35pm
Unfortunatley, the VerifierResult class doesn't have a constructor that takes the TargetInstance or a public setter for the TargetInstance property. The setter is internal, so there's currently no way to specify the entity. I suggest you post a feature request to the Data Verification forum.
 
http://www.ideablade.com/forum/forum_topics.asp?FID=26&title=data-verification-validation -



Print Page | Close Window