Silverlight Databinding Validation

Silverlight Databinding Validation

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


MBonner posted on Wednesday, August 05, 2009

I have some business objects that have Int32 fields. When I designed the fields, I used Int32 datatypes for the type of the backing property.

Now, when I databind those fields from a Silverlight form, I'd like to detect invalid input and display it as part of the broken rules/CSLA:PropertyStatus implementation. I.E. if the user enters "Bob" in the numeric entry textbox, I want a message to show up "Please enter a valid number!"

At the moment, the only way I can think to implement this type of rule is to change my backing property to a string type and then use a RegEx or a custom validation to detect if the string is a valid integer.

Is there a way to accomplish this without having to re-engineer my business object?

Thanks...

- Mike :-)

RockfordLhotka replied on Wednesday, August 05, 2009

This is a constant issue with data binding on any platform.

Silverlight 3 does have an answer, in that they have a basic validation mechanism that is triggered by an exception being thrown when attempting to set the property. I haven't really explored it, as it is a pure UI thing (the value never gets into the object obviously), but it may be helpful for your scenario?

MBonner replied on Saturday, August 08, 2009

Rocky,

Activating the SL3 validations is as easy as changing the data binding from:

Text="{Binding Path=G2Total, Mode=TwoWay"

To:

Text="{Binding Path=G2Total, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"

However, I now have two distinctive validation styles, the CSLA PropertyStatus exclamation point and the SL3 styled red frame with the little red triangle at the top and error messages that pop up when you re-enter the field. This is ok for my current prototype but I'd like to get to a point where I have a single style/error list. I'll probably take a look at your PropertyStatus code and see if I can push the errors into the SL3 Validation class's ErrorsProperty somehow, or pull the ErrorsProperty values into the CSLA PropertyStatus class.

In the meantime, if anyone else has already tackled this issue or has a proposed solution, please let me know.

Thanks...

- Mike :-)

RockfordLhotka replied on Saturday, August 08, 2009

I've thought about making CSLA work with the SL3 validation model - and I still might do so in version 3.8.

However, there are some very serious and real limitations to the SL3 model that make it undesirable. This is because it is an exception-based model, not an interface-based model.

This means that it works by handling any exception thrown when the setter is called. Nothing more complex than that.

The result of this is that it can only display one error, not multiples. And since the setters aren't being called on dependent properties, the entire dependent property validation concept goes away (changing one property can't cause another property to be shown as invalid).

So I haven't gotten too excited about supporting their model, because it is not a good model, and it would cripple some nice features of CSLA.

On the other hand, maybe that's OK - and it wouldn't be that terribly hard to support. I'd just change CheckRules() to throw an exception on the first broken rule (probably ignoring all warn/info severity rules entirely) and to not process any dependent property rules.

Again, very far from ideal, and so it isn't real likely that I'll do it.

Jack replied on Saturday, August 08, 2009

I haven't had time to look at the new Validation model but it had been on the back of my mind and I was waiting for a mention of it in the forums.

Can you not just set a 'reminderToThrowMSException flag', continue to process all the CSLA magic, then once you are done check the reminder flag and throw it?  Obviously only if a flag to 'throw MSExecption' was set by the developer...

It does seem a lot like all the other 'wizard' tools - does something blanket for the simple basic CRUD related activities but if you want anything custom then its a pain in the butt.

 

jack

RockfordLhotka replied on Saturday, August 08, 2009

I can absolutely throw an exception in CheckRules(), that’s not the hard part.

 

The hard part is that you’d STILL need to have the dual model (SL and PropertyStatus) or dependent properties wouldn’t work.

 

And you’d lose info/warn severities.

 

And only one of the rules would actually appear in the exception, even if several were broken.

Adam replied on Wednesday, August 12, 2009

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0in; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

I am currently having a problem with this issue where if an integer field is correctly populated (to show the accept button) then incorrectly populated with some text then the “accept changes” button is still available.

1.       Is it possible to get the accept button to disappear with the Silverlight validation as well as with the CSLA validation?

2.       I notice that you might be addressing this issue in CSLA 3.8 is this still the case?

3.       Any idea when CSLA light 3.8 will be publically available?


Many thanks

RockfordLhotka replied on Thursday, August 13, 2009

I don’t know that SL validation can address that issue. Maybe it can, but I don’t know how.

 

I don’t think I’ll be addressing this issue in 3.8, because I don’t know how to address it. If the data type doesn’t match, the value never gets through data binding to the business object, so the object doesn’t even know that an attempt was made to change the value. CSLA lives at the object level, so it is entirely unclear that CSLA can do anything about this at all.

 

What I'd mentioned I could do is to gut CSLA's capabilities and have CheckRules() throw an exception when a rule is broken. Basically disable all the CSLA goodness in favor of purely supporting the SL model. I rather doubt I'll do that though, because I don't see the value in abandoning a powerful model in favor of a much more limited model.

 

I’ll probably do a preview release of 3.8 to solicit feedback on some of the changes I’ve made to InvokeMethod and PropertyStatus late this week. I’m on vacation next week, and would like to get something online so people can play with it while I’m out of touch.

 

Rocky

Adam replied on Thursday, August 13, 2009

Thanks for the reply Rocky and I think your right stick with the powerful model :)

Look forward to trying 3.8 later this week and hope you have a nice hoilday.

Copyright (c) Marimer LLC