How Do I Trigger Validation ??

How Do I Trigger Validation ??

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


NickTaylor posted on Saturday, May 23, 2009

Hi, I wonder if anyone can help with this simple little problem. I am relitively new to CSLA and dot net, so forgive my ignorance!

I have a simple windows form which is essentially a maintenance form for a business object. My problem is that if I dont enter any data in a field, the validation rules are not triggered even though there are authorisation rules in the business object. In this instance its just a CommonRules.StringRequired rule, as the field cannot be empty.

If however, I enter some text and then delete it again, the rule is triggered and the error provider displays the appropriate warning icon.

Ideally when I try to Save the data to the business object I would like the validation rules to fire and identify any fields that contain invalid ( or empty ) data.

I'm sure this is really easy, but I dont know how to do it...!?

Any help would be gratefully received...

Many thanks,

Nick 

whelzer replied on Saturday, May 23, 2009

Its probably just your binding.  Right click your text box and Go to DataBindings , click Advanced and ensure that OnPropertyChanged is the binding type selected, (its probably OnValidation right now....)

NickTaylor replied on Saturday, May 23, 2009

Thanks for the advice, and you were correct about the setting of the data source update mode. I have now changed this to OnPropertyChanged, however the problem is still persisting...

Any other suggestions would be most welcome...

Thanks,

Nick

McManus replied on Saturday, May 23, 2009

Hi Nick,

Probably you don't call ValidationRules.CheckRules() in the DataPortal_Create() method. Therefore, the rules are not evaluated until a PropertyChanged event occurs.

Hope this helps!

Cheers,
Herman

NickTaylor replied on Sunday, May 24, 2009

Hi Herman,

Thanks, that certainly helps me out. Everytime I create a new record, I now see the form controls placed into their error state right from the start. Do you know if there is anyway that I could expose the call to ValidationRules.CheckRules() as a public method on the business object ? I could then call something like _boCustomer.CheckRules() as part of the forms Save() method code. This in turn would ensure that the _boCustomer.IsValid flag is set correctly and I can then pick up on this during the save method code.

By the fact that this functionality isn't there probably means my thinking is wrong, but it would seem useful to be able to trigger this as part of my Save() method code.

Thanks,

Nick

McManus replied on Sunday, May 24, 2009

Hi Nick,

You could make a public method Validate() or something like that, which in turn calls ValidationRules.CheckRules(), but that is a BAD idea.

The business object is responsible for maintaining its own state and must not depend on someone else for that.

Grtz,
Herman

NickTaylor replied on Sunday, May 24, 2009

Yes, I would agree on that one Herman.

How about I put a call to ValidationRules.CheckRules() within my DataPortal_Update() and DataPortal_Insert() methods. I could then throw an exception if any of the data is invalid. Does that make sense...?

Thanks,

Nick

mbblum replied on Sunday, May 24, 2009

The BO properties IsValid and IsSavable should be false until all validation rules pass. This is managed by the CSLA fraamework.

If you are not getting this functionality, then my suggestion is to review the samples with CSLA to observe how they achieve this.

When IsSavable is false, the CSLA framework on a call to Save the BO will through an exception and not call Dataportal_XYZ, i.e. it is too late to be calling CheckRules in those methods.

Copyright (c) Marimer LLC