PDA

View Full Version : Validation sample please!


CNemo7539
06-13-2007, 02:46 AM
Hello all!

I will appreciate if somebody can provide me with very basic example of how to setup validation.

I have following code, but every time i run it i get exception saying that context cannot be created. If remove all stuff related to validation then I'm able to create object.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>

<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<applicationSettings>
</applicationSettings>

<spring>
<Settings>
<setting name="Test" serializeAs="String">
<value>Mike Test it</value>
</setting>
</Settings>


<context>
<resource uri="config://spring/objects"/>
</context>

<objects xmlns="http://www.springframework.net"
xmlns:v="http://www.springframework.net/validation">

<object name="MyPolicy" type="SpringValidation.Policy, SpringValidation">
</object>


<v:group id="PolicyValidator">
<v:required id="FirstNameRequired" test="FirstName" />
<v:ref name="Test"/>
</v:group>



<v:group id="Test">
<v:required id="LastNameRequired" test="LastName"/>
</v:group>



</objects>

</spring>
</configuration>


public class Policy
{
private string _firstName;
public string FirstName
{
get{return _firstName;}
set{_firstName = value;}
}
private string _lastName;
public string LastName
{
get{return _lastName;}
set{_lastName = value;}
}

}




private void button1_Click(object sender, EventArgs e)
{
IApplicationContext context = ContextRegistry.GetContext();
Policy pol = (Policy)context.GetObject("MyPolicy");
pol.FirstName = "Jon";
IValidator val = (IValidator)context.GetObject("PolicyValidator");
ValidationErrors errors = new ValidationErrors();
val.Validate(pol, errors);
}

Could somebody create stripped example with step-by-step instructions?
There is not a lot of sense in my code but i would like to be able reference validators from each other.
Another question is how i can create error messages in configuration.
And it will be nice to read about providers as well.

Thanks on beforehand!

Bruno Baia
06-13-2007, 10:10 PM
Hi,

I created an issue about this in JIRA (http://opensource.atlassian.com/projects/spring/browse/SPRNET-587).
There is an example in the SpringAir sample <Web Project>/Web/BookTrip/Web.Config


HTH,
Bruno

CNemo7539
06-15-2007, 02:45 PM
Thanks a lot.

I already figured it out. Coded everything from scratch on the other computer and it worked for me. It was on Win XP.
Returned home and tryed to do the same under Vista. I'm kind of get it work after sever attempts, but before that I've disabled UAK.

I wonder if there any relation with Vista security. Was it tested under Vista.
I definitly can say that default installation path into Program Files doesn't allow to build solution after install until one disable UAK.