Newbie: Validation question

Newbie: Validation question

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


bgilbert posted on Wednesday, November 29, 2006

I'm very new to CSLA, so forgive me if this is an overly simple question.

It appears that validation rules methods are only called when setting a property. However, if a property set is not called, there is no broken rule. If I then do an insert to the database, it's up to the database to validate values.
As an example, in PTracker's Edit Roles form, you can create a new record (changing the key field), leave the name property blank, and save the record, even though there is StringRequired rule on the name property. Calling ValidationRules.CheckRules doesn't seem to trap this.

Any help is appreciated.

Barry

ajj3085 replied on Wednesday, November 29, 2006

Barry,

Not exactly sure what the problem is, but that should work fine.  Have you changed anything in the sample?

I use stringrequired and ValidationRules.CheckRules all the time, and it works fine for me.

Andy

bgilbert replied on Wednesday, November 29, 2006

Andy,
Thanks for your reply.
No, we didn't change anything in PTracker.

Maybe I don't completely understand how this would work. Does CheckRules check all properties even if the Property Set methods haven't been called? In other words, does it evaluate the rules against the default values?

Barry

xal replied on Wednesday, November 29, 2006

You should always define a default value in the variable's declaration.
CheckRules() checks the rules for ALL properties, regardless they were set by the user or not.

If I understand correctly, your problem araises with new objects. In that case, you should define a DataPortal_Create and call CheckRules() there.

Andrés

bgilbert replied on Wednesday, November 29, 2006

Yes, we always initialize variable when they're declared. We've chosen not to use DataPortal_Create because we have no need to initialize objects with defaults other than from their declarations.

I know this is simple and I'm probably missing something. If you have a minute, please try this test:
In PTracker, open the RolesEdit form.
Go to a new row in the DataGridView. It fills a default id. Modify this id to anything other than any existing id.
Click on a different row in the grid. Notice that it doesn't un-dirty the new row.
Click the Save button. In our tests, it saves the new record even though the name property is blank and there is a StringRequired rule on the property.

If anyone gets a different result, I've obviously mucked something up and I apologize for wasting your time.

Thanks in advance,
Barry

xal replied on Wednesday, November 29, 2006

If you don't want to use dp_create you could do the same thing in your factory method:

Public Shared Function NewItem()  As Item
Dim obj as New Item
obj.MarkNew()
obj.CheckRules()
Return obj
End Function


Andrés

bgilbert replied on Wednesday, November 29, 2006

Thanks again for your suggestions. Just to confirm that this isn't the result of something I screwed up, I re-downloaded the current versions of CSLA & PTracker. The test I mentioned previously still fails. Here's the test:

1-In the RolesEdit form, add a new row in the grid.
2-Change the Id value of the new row to anything new. Leave the name field blank.
3-Click on a different row in the grid.
4-Click Save.

It saves the record without a name value. Is this an oversight in PTracker, an oversight in CSLA, or something I'm just not understanding?

Barry

ajj3085 replied on Thursday, November 30, 2006

I'll fire up the sample later, but I'm inclined to think its something with the sample, since I'm using 2.1.1 (I assume that's the version you're looking at?) and my string required fields are correctly preventing a save.

Andy

Jimbo replied on Sunday, December 03, 2006

There may be a connection between the behaviour you are reporting and the problem that I presented in post 9590
"BusinessBase.PropertyHasChanged calling ValidationRules.CheckRules in Version 2.1.1"

That is essentially:  Validation.CheckRules works fine when you call it direct but not when it is called from PropertyHasChanged.

Jimbo.



ajj3085 replied on Wednesday, November 29, 2006

It may be a problem with the sample application itself; do as Andres suggests and see if the DP_C for the class in question is calling ValidationRules.CheckRules.

Andy

bgilbert replied on Sunday, December 03, 2006

I'm not so sure. What I'm talking about is that it seems that, in
PTracker, the CheckRules method isn't called when no property sets are
called. I've tried putting in a CheckRules at the beginning of the Roles
class's Insert method and it calls the validation routines, but it never
bubbles a broken rule back to the UI.

Being still somewhat unfamiliar with all the details of the CSLA, I'm
not sure if it is just something to do with the sample app or with my
understanding.

Barry

>>> "Jimbo" 12/03/06 5:07 AM >>>
There may be a connection between the behaviour you are reporting and
the problem that I presented in post 9590
"BusinessBase.PropertyHasChanged calling ValidationRules.CheckRules in
Version 2.1.1"

That is essentially: Validation.CheckRules works fine when you call it
direct but not when it is called from PropertyHasChanged.

Jimbo.





mr_lasseter replied on Sunday, December 03, 2006

Barry,

The problem is with the sample app.  When the role class is created thier is no call to the CheckRules Function.

    Friend Shared Function NewRole() As Role

      Return New Role

    End Function

Should be:

    Friend Shared Function NewRole() As Role
      Dim newRole as New Role

      newRole.CheckRules
      Return newRole

    End Function

Mike

Copyright (c) Marimer LLC