PDA

View Full Version : Windows Forms and Spring.Validation framework?


mov
11-20-2006, 10:26 PM
Hi
I have just started using Spring and I quite impressed :-)
We are doing ClickOnce applications and have made our own framework for doing validation etc.

Are there any samples showing how to use the Spring validation framework with Windows Forms???

Thanks in advance - and thanks for a great framework..

Regards Morten :)

Bruno Baia
11-25-2006, 06:15 PM
Hi,


The Spring.NET Validation Framework is designed in a way that enables data validation in different application layers using the same validation rules.


As it's not tied to a specific presentation technology, you can take a look to the SpringAir sample that is using it.


<v:group id="tripValidator">

<v:required id="departureAirportValidator" test="StartingFrom.AirportCode">
<v:message id="error.departureAirport.required" providers="departureAirportErrors, validationSummary"/>
</v:required>

<v:group id="destinationAirportValidator">
<v:required test="ReturningFrom.AirportCode">
<v:message id="error.destinationAirport.required" providers="destinationAirportErrors, validationSummary"/>
</v:required>
<v:condition test="ReturningFrom.AirportCode != StartingFrom.AirportCode" when="ReturningFrom.AirportCode != ''">
<v:message id="error.destinationAirport.sameAsDeparture" providers="destinationAirportErrors, validationSummary"/>
</v:condition>
</v:group>

<v:group id="departureDateValidator">
<v:required test="StartingFrom.Date">
<v:message id="error.departureDate.required" providers="departureDateErrors, validationSummary"/>
</v:required>
<v:condition test="StartingFrom.Date >= DateTime.Today" when="StartingFrom.Date != DateTime.MinValue">
<v:message id="error.departureDate.inThePast" providers="departureDateErrors, validationSummary"/>
</v:condition>
</v:group>

<v:group id="returnDateValidator" when="Mode == 'RoundTrip'">
<v:required test="ReturningFrom.Date">
<v:message id="error.returnDate.required" providers="returnDateErrors, validationSummary"/>
</v:required>
<v:condition test="ReturningFrom.Date >= StartingFrom.Date" when="ReturningFrom.Date != DateTime.MinValue">
<v:message id="error.returnDate.beforeDeparture" providers="returnDateErrors, validationSummary"/>
</v:condition>
</v:group>

</v:group>



Hope this helps,
Bruno

Mark Pollack
11-27-2006, 05:21 PM
Hi,

No sample yet, like Bruno said. However, if you end up using it for your WinForm app and uncover any useful tidbits/best practices, please let us know.

Mark

Aleks Seovic
11-28-2006, 04:44 PM
While the validation framework is completely presentation agnostic and can be used within WinForms apps just as easy as within ASP.NET apps, as both Bruno and Mark suggested, I'm still not quite certain what would the best usage pattern under WinForms.

I think the best way to do it would be similar to what we are doing in ASP.NET, where all the validators you need are injected into the form, and then you invoke validation manually within the appropriate event handler (when submitting the form, for example).

Once we get Windows Forms module of Spring.NET done all of this will hopefully be figured out, but in the meantime if you can play with validation within WinForms and let us know what did and did not work for you we'd very much appreciate it.

Thanks,

Aleks

harald
12-20-2006, 05:01 PM
I have this working quite nicely, here is what i did:

1.) I derived an ErrorProviderErrorsRenderer from System.Windows.Forms.ErrorProvider that is an IExtenderProvider for any type of Controls, IMessageSourceAware and it can hold an IValidator

2.) Simply drop the ErrorProviderErrorsRenderer on a Form of your choice

3.) Inject message source and validator

4.) Now you can set a provider name for every component on the Form that has to match the providers attribute of your xml validators definition. If no provider string is specified the property name of the binding source is used (which ought to be the name of the validated property of your business object, thus setting the providers attribute to the propertyname in your xml definition would be a good choice)

5.) Because the ErrorProviderErrorsRenderer is an IExtenderProvider for Components it knows all the UI controls on the Form. Thus a simple Validate() call on the ErrorProviderErrorsRenderer is sufficient to validate the business object and display the ErrorsRenderer icons right next to the controls in question.

6.) Because there is still some setup overhead involved I derived a BaseForm from System.Windows.Forms.Form that is IApplicationContextAware and injects the ApplicationContext instance to every other IApplicationContextAware control on it. Thus configuring the ErrorProviderErrorsRenderer is as simple as entering the validators name in the Forms designer.

7.) To act fully transparent in the background I overloaded OnClosing() in my BaseForm class to automatically do the validation if the Forms accept key is pressed and suppress closing of the form if the validation fails.

Making your WinForms validateable is now a matter of changing the base class (besides defining the validators for your business objects of course).

The code is here (http://springutils.fluffnstuff.org/browser/trunk/SpringUtils/Windows).

regards,
harald.

vikram.shankar
03-06-2007, 10:41 AM
Hi Harald,

Thanks a lot for a detailed description.
I went through the code that you have posted. I could not build them to understand completely.

Could you please help me to configure your example....

thanks a lot for your help.

regards,
Vikki