A CSLA bug or I missed something?

A CSLA bug or I missed something?

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


Brian Tian posted on Thursday, August 31, 2006

Hi, All.

In my base object, I have start date and end date propery.

private SmartDate _startDate = new SmartDate("",true ) ;
private SmartDate _enddate = new SmartDate("",false );

public string StartDate
{
      
get
      

            CanReadProperty(
"StartDate", true);
            
return _startDate.Text;
      }
      
set
      
{
            CanWriteProperty(
"StartDate", true);
            
if (!_startDate.Equals(value))
            {
                  _startDate.Text =
value;
                  PropertyHasChanged(
"StartDate");
            }
      }
}

EndDate property is similiar as StarDate property and I won't repeat here.

I set the validation rule to force the end date is greater the start date.

ValidationRules.AddRule(CommonRules.StringRequired, "StartDate");
ValidationRules.AddRule(
CommonRules.StringRequired, "EndDate");

ValidationRules.AddRule(BeginDateGTEndDate, "StartDate");
ValidationRules.AddRule(BeginDateGTEndDate, "EndDate");

the function BeginDateGTEndDate to return bool value
private bool BeginDateGTEndDate( Object target , Csla.Validation.RuleArgs e)
{
      
if (_startDate > _enddate) 
      {
            e.Description =
"Beginning Date is greater than End Date";
            
return false;
      }
      
else
            
return true ;
}

For example, the startDate is 8/16/2006. EndDate is 8/20/2006. it is fine and will be saved. If I modified endDate to 8/15/2006 and still keep the old start date, it will failed as it should be.  If I changed start date to any date earlier than 8/15/2006. for example, 8/10/2006, it will be failed(in this case, I modify both start date and end date). Why? it should be ok because  8/10/2006 < 8/15/2006, and the function  BeginDateGTEndDate return true. I tracked the code and find base.IsValid = false.  I already modified the date and start date is less than end date. so, I find if I modified both properties and even  BeginDateGTEndDate( ) return true,  the object could not be saved.

 If I modified on side, it will be ok. for example, the startDate is 8/16/2006. EndDate is 8/20/2006. it is fine and will be saved. if I modified end date to 8/5/2006, it will be failed as it should be. if I modified end date back to any date greater than start date (in the case, start date doesn't changed at all, still is 8/16/206). it will be ok and be saved. 

Why I can not modify both property? Did I miss something or this is a CLAS bug?

Thanks for help

Brian

 

 

ajj3085 replied on Thursday, August 31, 2006

It seems whats happening is this;

You have two broken rules indicating that the startdate is after the end date.  On of the broken rules is for StartDate, the other for Enddate.  The problem is that your property setter is only causing one of those rules to be rechecked and removed; the other remains.

The solution depends on which version of Csla you're using; if its the new 2.1 beta refresh, you can use the AddDependantProperty method, which will cause both properties to be checked.

Alternately (if you're using 2.0.3) you'll need to call ValidationRules.CheckRules( "StartDate" ) in your EndDate setter, and do the reverse in the setter for StartDate.

HTH
Andy

Brian Tian replied on Thursday, August 31, 2006

Andy,

Thank you, I'm use (CSLA 2.03). I did what you told me but I still have the same problem. At least, I know what kind of the problem it is.

Thank you very much and have a great one.

Brian

The code snippet like this

public string StartDate
{
      
get
      

            CanReadProperty(
"StartDate", true);
            
return _startDate.Text;
      }
      
set
      
{
            CanWriteProperty(
"StartDate", true);
            
ValidationRules.CheckRules("EndDate");
            
if (!_startDate.Equals(value))
            {
                  _startDate.Text =
value;
                  PropertyHasChanged(
"StartDate");
            }
      }
}

public string EndDate
{
      
get
      
{
            CanReadProperty(
"EndDate", true);
            
return _enddate.Text;
       }
      
set
      
{
               CanWriteProperty(
"EndDate", true);
               ValidationRules.CheckRules(
"StartDate");
               
if (!_enddate.Equals(value))
               {
                        _enddate.Text =
value;
                        PropertyHasChanged(
"EndDate");
               }
         }
}

DesNolan replied on Thursday, August 31, 2006

Not too familar with the use of  ValidationRules.CheckRules...
 
but it appears you may  not be doing it at the end o the setter as previously advised, you're calling the check rules before you set  the value, try

public string StartDate
{
      
get
      

            CanReadProperty(
"StartDate", true);
            
return _startDate.Text;
      }
      
set
      
{
            CanWriteProperty(
"StartDate", true);
            if (!_startDate.Equals(value))
            {
                  _startDate.Text =
value;
                
ValidationRules.CheckRules("EndDate");
                  PropertyHasChanged("StartDate");
            }
      }
}

OR EVEN

public string EndDate
{
      
get
      
{
            CanReadProperty(
"EndDate", true);
            
return _enddate.Text;
       }
      
set
      
{
               CanWriteProperty(
"EndDate", true);
               if (!_enddate.Equals(value))
               {
                        _enddate.Text =
value;
                        PropertyHasChanged(
"EndDate");
                        ValidationRules.CheckRules("StartDate");
               }
         }
}

Cheers,

Des Nolan

Brian Criswell replied on Friday, September 01, 2006

DesNolan:
Not too familar with the use of  ValidationRules.CheckRules...
 
but it appears you may  not be doing it at the end o the setter as previously advised, you're calling the check rules before you set  the value, try

public string StartDate
{
      
get
      

            CanReadProperty(
"StartDate", true);
            
return _startDate.Text;
      }
      
set
      
{
            CanWriteProperty(
"StartDate", true);
            if (!_startDate.Equals(value))
            {
                  _startDate.Text =
value;
                
ValidationRules.CheckRules("EndDate");
                  PropertyHasChanged("StartDate");
            }
      }
}

OR EVEN

public string EndDate
{
      
get
      
{
            CanReadProperty(
"EndDate", true);
            
return _enddate.Text;
       }
      
set
      
{
               CanWriteProperty(
"EndDate", true);
               if (!_enddate.Equals(value))
               {
                        _enddate.Text =
value;
                        PropertyHasChanged(
"EndDate");
                        ValidationRules.CheckRules("StartDate");
               }
         }
}

Cheers,

Des Nolan



You do it the other way because the validation check has to come before the PropertyChanged event raised by PropertyHasChanged().

Brian Tian replied on Friday, September 01, 2006

 

It works now.  :-)

Thanks for you guys help.

Brian Criswell replied on Sunday, September 03, 2006

Brian Criswell:
DesNolan:
Not too familar with the use of  ValidationRules.CheckRules...
 
but it appears you may  not be doing it at the end o the setter as previously advised, you're calling the check rules before you set  the value, try

public string StartDate
{
      
get
      

            CanReadProperty(
"StartDate", true);
            
return _startDate.Text;
      }
      
set
      
{
            CanWriteProperty(
"StartDate", true);
            if (!_startDate.Equals(value))
            {
                  _startDate.Text =
value;
                
ValidationRules.CheckRules("EndDate");
                  PropertyHasChanged("StartDate");
            }
      }
}

OR EVEN

public string EndDate
{
      
get
      
{
            CanReadProperty(
"EndDate", true);
            
return _enddate.Text;
       }
      
set
      
{
               CanWriteProperty(
"EndDate", true);
               if (!_enddate.Equals(value))
               {
                        _enddate.Text =
value;
                        PropertyHasChanged(
"EndDate");
                        ValidationRules.CheckRules("StartDate");
               }
         }
}

Cheers,

Des Nolan



You do it the other way because the validation check has to come before the PropertyChanged event raised by PropertyHasChanged().


Sorry, I meant that the first one is the one I would choose so that _endDate has the correct value and the rule check is made before the PropertyChangedEvent.

Copyright (c) Marimer LLC