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!
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!