Hello All,
I am getting the following exception in my debugger (instance methods of the target object not allowed).
I want to write a CustomRule and I am following the method described in ProjectTracker example.This is what I have so far.
protected
override void AddBusinessRules(){
ValidationRules.AddRule(YearRange,
"Year");}
private bool YearRange(object target, Csla.Validation.RuleArgs e){
if (_year < 2006 || _year > DateTime.Now.Year){
e.Description =
"Year range is out of bounds"; Logger.Bug("Year range is out of bounds"); return false;}
else return true;}
Now in the Property I have
private
Int16 _year = 0;[System.ComponentModel.
DataObjectField(false, false, false)] public Int16 Year{
[System.Runtime.CompilerServices.
MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] get{
CanReadProperty(
"Year", true); return _year;}
[System.Runtime.CompilerServices.
MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] set{
CanWriteProperty(
"Year", true); if (_year != value){
_year =
value;ValidationRules.CheckRules(
"Year");PropertyHasChanged(
"Year");}
}
}
Thanks in Advance
I'm not sure exactly which version of Csla you're using but my rule methods tend to be static and the target being passed in contains the object instance you'll be testing.
That is, you'd be looking at:
private static bool YearRange(object target, Csla.Validation.RuleArgs e)
{
if (target._year < 2006 || target._year > DateTime.Now.Year)
{
e.Description = "Year range is out of bounds";
Logger.Bug("Year range is out of bounds");
return false;
}
else
return true;}
Also, you should note that you don't have to issue a ValidationRules.CheckRules("Year"); when you've already said that the property has changed, as it should test any rules you have applied to the "Year" property.
You might also want to consider accomodating your default value of 0 in the rule. Is it valid? Is it not valid? Does 0 mean empty? Maybe you should consider using a nullable integer as a backing field.
hth,
Chris
Thanks Chris for your response but now I am getting Error 2 Argument '1': cannot convert from 'method group' to 'Csla.Validation.RuleHandler' error.
I have searched the forum for answers and right now too confused because I found too many ways and none worked for me so I went back to the book.
I started out with 2.0 and now using 3.0 and the book Im refering to is Expert C# 2005.
I am doing UI validation as well and that is totally independent but all the BO's needs its own vlaidation so that the broken rules collection can be used if UI validation logic is not prefered by someone who wants just my BO's.
Thanks again.
Hmm..
Here's an example I have:
In AddBusinessRules:
ValidationRules.AddRule<Individual>(IsBirthYearValid, _birthYear); (I'm using 3.5 managed properties)
And the Rule Method (note the target is of the type the rule is being added for)
private static bool IsBirthYearValid(Individual target, Csla.Validation.RuleArgs e)
{
int? birthYear = target.ReadProperty<int?>(_birthYear);
if ((birthYear.HasValue) &&
(birthYear < 1900 || birthYear > DateTime.Today.Year + 1))
{
e.Description = BusinessRules.InvalidBirthYear;
return false;
}
return true;
}
I tried to just add a rule like this:
ValidationRules.AddRule(IsBirthYearValid, "BirthYear"); <-- this doesn't compile
and I received probably the errors you were getting. To fix it, you need to add the type parameter of the "target" you're checking.
ValidationRules.AddRule<Individual>(IsBirthYearValid, "BirthYear"); <-- This compiles.
Thank a Million works perfectly!!!Added the type for the target and everything works superbly.I need to by the new copy of 3.5 book.
Thanks again Chris.
Copyright (c) Marimer LLC