Suppress Validation Exceptions for Csla.CommonRules Validation Methods

Suppress Validation Exceptions for Csla.CommonRules Validation Methods

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


timdilbert posted on Saturday, September 12, 2009

Quick question; I have a business object and I'm trying to add a few of the default Csla built-in Validation methods but every time one of them is broken, it throws an exception.

I dont want exceptions to be thrown, instead the rule to be broken and it add the rule to the BrokenRuleCollection on the object. HELP!!! *See example below*

//-> EMAIL ADRESS
ValidationRules.AddRule(CommonRules.StringRequired, new RuleArgs(EmailAddressProperty, RuleSeverity.Warning, false));

So when I update the EmaillAddress property..

object.EmailAddress = string.Empty;

The following exception is thrown;

Property load or set failed for property Email Address (Validation rule rule://Csla.Validation.CommonRules/StringRequired/Email Address failed in property Email Address)

JonnyBee replied on Sunday, September 13, 2009

Hi,

What version of Csla are you using and what is the code for your EMailAddress property.

Seems to me that there is:
a: mismatch between the actual propertyname and the name registered i PropertyDescriptor (=> leads to rule not beeing able to find property)
b: AuthorizationRules / override of CanReadProperty/CanWriteProperty  protects the property from beeing read/set.

/jonnybee

timdilbert replied on Sunday, September 13, 2009

I'm using version 3.6.1-090204 of Csla.

I thought that i had these declared properly, below is the code for the EmailAddress Property

private static PropertyInfo EmailAddressProperty = RegisterProperty(typeof(ColUser), new PropertyInfo("Email Address"));

Here is the getter/setter for the EmailAddress property (nothing to it really).

public string EmailAddress
{
get { return GetProperty(EmailAddressProperty); }
set { SetProperty(EmailAddressProperty, value); }
}//;

Now with regards to the editing of the object and permissions go. I never did set it on the per property level, just on the AllowExecute for the methods, so

protected override void AddAuthorizationRules()
{
string saveMethodName = "Save";
string deleteMethodName = "Delete";

if (IsNew)
AuthorizationRules.AllowExecute(saveMethodName, new List() { Permissions.CreateUser }.ToArray());
else if (!IsNew)
AuthorizationRules.AllowExecute(saveMethodName, new List() { Permissions.ModifyUser, Permissions.CreateUser }.ToArray());


AuthorizationRules.AllowExecute(deleteMethodName, new List() { Permissions.DeleteUser }.ToArray());
base.AddAuthorizationRules();
}

JonnyBee:
Hi,

What version of Csla are you using and what is the code for your EMailAddress property.

Seems to me that there is:
a: mismatch between the actual propertyname and the name registered i PropertyDescriptor (=> leads to rule not beeing able to find property)
b: AuthorizationRules / override of CanReadProperty/CanWriteProperty  protects the property from beeing read/set.

/jonnybee

JonnyBee replied on Sunday, September 13, 2009

Hi,

Your error is easy to spot:

private static PropertyInfo EmailAddressProperty = RegisterProperty(typeof(ColUser), new PropertyInfo("Email Address"));

public string EmailAddress
{
get { return GetProperty(EmailAddressProperty); }
set { SetProperty(EmailAddressProperty, value); }
}//;

Your property is NOT named "Email Address", the property name should be "EmailAddress".
PropertyInfo class has several overloads but the first parameter must always be the exact PropertyName - second parameter may be "friendely property name". So your PropertyInfo should look like this:

private static PropertyInfo EmailAddressProperty = RegisterProperty(typeof(ColUser), new PropertyInfo( "EmailAddress", "Email Address"));

/jonnybee


timdilbert replied on Monday, September 14, 2009

You were right, sorry about this..

JonnyBee:
Hi,

Your error is easy to spot:

private static PropertyInfo EmailAddressProperty = RegisterProperty(typeof(ColUser), new PropertyInfo("Email Address"));


public string EmailAddress

{

get { return GetProperty(EmailAddressProperty); }

set { SetProperty(EmailAddressProperty, value); }

}//;


Your property is NOT named "Email Address", the property name should be "EmailAddress".
PropertyInfo class has several overloads but the first parameter must always be the exact PropertyName - second parameter may be "friendely property name". So your PropertyInfo should look like this:


private static PropertyInfo EmailAddressProperty = RegisterProperty(typeof(ColUser), new PropertyInfo( "EmailAddress", "Email Address"));


/jonnybee


rsbaker0 replied on Sunday, September 13, 2009

timdilbert:
...
The following exception is thrown;

Property load or set failed for property Email Address (Validation rule rule://Csla.Validation.CommonRules/StringRequired/Email Address failed in property Email Address)


If you get his error, you're actually getting a runtime exception in the validation rule code itself, not because the validation check itself is deciding the rule doesn't pass.

In the debugger, bring up the Exceptions screen and tell it you want to break when an exception is "thrown", not just unhandled. Then it will actually stop at the point of failure and you can see what is wrong.

Copyright (c) Marimer LLC