ValidationRule not firing up when calling SetProperty (xxxxxProperty ) in CSLA 3.8

ValidationRule not firing up when calling SetProperty (xxxxxProperty ) in CSLA 3.8

Old forum URL: forums.lhotka.net/forums/t/11598.aspx


Lord02 posted on Tuesday, September 25, 2012

Hi Lhotka,

Could you please explain something to me ...

A rule I created is NOT firing up when I change the property which the rule is set on,

the problem is that calling this:   SetProperty(userEntryProperty, value);
does NOT fire up the rule ???! 

Example:

This is the class declaration ( the LogOnCredentials then inherits from BusinessBase<T> )
[Serializable]
    public class UserVerificationCredentials : LogOnCredentials<UserVerificationCredentials>

 

The csla-variable & Property look like this:

private static PropertyInfo<string> userEntryProperty = RegisterProperty<string>(c => c.UserEntry);
 
        public string UserEntry
        {
            get
            {
                return GetProperty(userEntryProperty);
            }
            set
            {
                SetProperty(userEntryProperty, value);
            }
        }

And it's wired up like this ( and this runs when the object is created in the static constructor ):
internal static UserVerificationCredentials NewUserVerificationCredentials()
        {
            return DataPortal.Create<UserVerificationCredentials>();
        }
        
        #endregion
        #region  Data Access
 
        /// <summary>
        /// Called by the dataportal for Create operations.
        /// </summary>
        /// <remarks>
        /// Runs on the dataportal layer if available.
        /// </remarks>
        [RunLocal]
        protected override void DataPortal_Create()
        {
            using (BypassPropertyChecks)
            {
                // Set properties tat
                // TODO: Evaluate fetching from HTTP context
                ValidationRules.CheckRules();
            }
        }
        #endregion

 /// <summary>
        /// Adds the business rules.
        /// </summary>
        protected override void AddBusinessRules()
        {
            ValidationRules.AddDataAnnotations();
            base.AddBusinessRules();
 
            ValidationRules.AddRule<UserVerificationCredentials>(UserEntryRule, userEntryProperty, 0);
        }



And this is the actual rule:
private static bool UserEntryRule<T>(T target, RuleArgs e) where T : UserVerificationCredentials
        {
            bool result = true;
            if (string.IsNullOrEmpty(target.ReadProperty(userEntryProperty)) == false)
            {
                // It should only be valid to enter a user entry when the 
                // control has already been activated.
                result = target.HasBeenActivatedByUser;
                e.Description = Resources.BusinessResources.SmsCodeRequired;
                e.Severity = RuleSeverity.Error;
            }
 
            return result;
        }

Do you have any idea why the rule is NOT being fired up when calling the:
SetProperty(userEntryProperty, value);    ???

Kind regards,
EE.
 

Lord02 replied on Tuesday, September 25, 2012

Nevermind ... I found out what the problem was.

The value was the same as before, hence the rule doesn't fire up! ;-)

rgd,

EE.

Copyright (c) Marimer LLC