Custom validator

G

Giuseppe Esposito

I don't know if it's the right name: I try to explain what I want to do.

Follow me. Wb.config file permits user to create personalized section.
The section can be read throught your ConfigurationSection's derived class
and each
element of the section should belong to a ConfigurationElement's derived
class.

Well. For standard field that return string, integer and so on, for each
attribute
you can use XXXXValidator to check if passed value is in a certain range.
That's fine, but I want to implement my own XXXX Validator.

So what I did is the following.

public sealed class ContentTypeValidator : ConfigurationValidatorBase
{
public override void Validate(object value)
{
ArrayList ct = new ArrayList();

ct.Add("text/plain");
ct.Add("text/HTML");
ct.Add("image/GIF");
ct.Add("image/JPEG");
ct.Add("image/TIFF");
ct.Add("application/pdf");
ct.Add("application/zip");
ct.Add("application/x-excel");
ct.Add("application/rtf");

try
{
if (Array.IndexOf(ct, value) == -1)
throw new ConfigurationErrorsException("Value isn't allowed");

}
catch (ConfigurationErrorsException err)
{
throw err;
}
}
}

I don't know if this is the right way to do what I want, but despite the
class doesn't contain
any formal error, if I got closest to my attribute declaration and press
[ and look for my Validator
class, it isn't there. What's the matter?


Thanks to all
Giusepp
 
N

Nathan Sokalski

I think that you are overcomplicating things for yourself. There is a
Control called CustomValidator. It has an event called ServerValidate that
is triggered when the control is validated, in which you can place any code
you want. To specify whether it passed validation, do one of the following:

args.IsValid=True

OR

args.IsValid=False

Hopefully this helps, Good Luck!
 
G

Giuseppe Esposito

Well, I need to use it in the web.config file, not in an asp page, so I suppose
that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe
 
N

Nathan Sokalski

Well, first of all, the web.config file is an XML file, so you won't be able
to write VB.NET or C#.NET code in it to begin with. The web.config file is
simply a list of settings, so I think I am a little confused about what you
are planning to do with your validation code. If you simply want to write a
class, create a .VB or .CS file (depending on what language you are using).
If you simply want a function or sub that does the validation, you can
either write a class containing nothing except that function or sub, or you
could add the function or sub to the Global class (located in the
Global.asax.vb or Global.asax.cs file). If you could try to clarify what you
are planning on doing with this validator, it might help me and anyone else
figure out what you are looking for. (NOTE: When posting a Reply, it is good
to include the previous message so that people can see anything you have
already tried, mentioned, as well as what the original problem was)
 
A

andrea

Sorry, I just saw that the name and e-mail used for posting is wrongly changed
with another profile.
By the way.

Well, I believe to have said all on the first post, but let me try to explain
it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>


Then ... to handle mySection I wrote a class that inherits from ConfigurationSection
in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you must define
each property of Item, otherwise compiler generate an error during the startup.

So here it is the class.


public class Item : ConfigurationElement
{
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

Now you want to be sure that name is in a certain range if it is numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case of
the string you probably use the stringValidator, right?

But StringValidator perform a RegEx search, so if you want to match your
value with something that is in a list, this isn't suit your goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}

public override ConfigurationValidatorBase ValidatorInstance
{
get
{
return new NameValidator(""); // Here there is a problem
}
}
}

public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};

public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator);
}

public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}



Basically it should be the same that a XValidator does, but there is one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item arrive
to my validator.

Bye
Andrea


NS> Well, first of all, the web.config file is an XML file, so you won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a .VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or sub,
NS> or you could add the function or sub to the Global class (located in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it might
NS> help me and anyone else figure out what you are looking for. (NOTE:
NS> When posting a Reply, it is good to include the previous message so
NS> that people can see anything you have already tried, mentioned, as
NS> well as what the original problem was)
NS>
NS> NS>
 
N

Nathan Sokalski

I think that you're looking for someone who knows a little bit more about
the communication between web.config and the code pages than I do. I have
never dealt with all of the parts of the web.config file you mention, so you
should probably try to find someone who has more experience with them than
me. Sorry I couldn't be of more help.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

andrea said:
Sorry, I just saw that the name and e-mail used for posting is wrongly
changed with another profile.
By the way.

Well, I believe to have said all on the first post, but let me try to
explain it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>


Then ... to handle mySection I wrote a class that inherits from
ConfigurationSection in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you must
define each property of Item, otherwise compiler generate an error during
the startup.

So here it is the class.


public class Item : ConfigurationElement {
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

Now you want to be sure that name is in a certain range if it is numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case of
the string you probably use the stringValidator, right?

But StringValidator perform a RegEx search, so if you want to match your
value with something that is in a list, this isn't suit your goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}

public override ConfigurationValidatorBase ValidatorInstance { get
{
return new NameValidator(""); // Here there is a problem
}
}
}

public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};

public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator); }

public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}



Basically it should be the same that a XValidator does, but there is one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item
arrive to my validator.

Bye
Andrea


NS> Well, first of all, the web.config file is an XML file, so you won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a .VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or sub,
NS> or you could add the function or sub to the Global class (located in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it might
NS> help me and anyone else figure out what you are looking for. (NOTE:
NS> When posting a Reply, it is good to include the previous message so
NS> that people can see anything you have already tried, mentioned, as
NS> well as what the original problem was)
NS> NS>
 
A

andrea

I found the solution .... I'll post it on my blogs, but it's in italian only.
Maybe I'll translated it in english.

Bye
Andrea

NS> I think that you're looking for someone who knows a little bit more
NS> about the communication between web.config and the code pages than I
NS> do. I have never dealt with all of the parts of the web.config file
NS> you mention, so you should probably try to find someone who has more
NS> experience with them than me. Sorry I couldn't be of more help.
NS>
NS> NS>
Sorry, I just saw that the name and e-mail used for posting is
wrongly
changed with another profile.
By the way.
Well, I believe to have said all on the first post, but let me try to
explain it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>
Then ... to handle mySection I wrote a class that inherits from
ConfigurationSection in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you
must
define each property of Item, otherwise compiler generate an error
during
the startup.
So here it is the class.

public class Item : ConfigurationElement {
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
Now you want to be sure that name is in a certain range if it is
numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case
of
the string you probably use the stringValidator, right?
But StringValidator perform a RegEx search, so if you want to match
your value with something that is in a list, this isn't suit your
goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}
public override ConfigurationValidatorBase ValidatorInstance { get
{
return new NameValidator(""); // Here there is a problem
}
}
}
public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};
public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator); }
public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}
Basically it should be the same that a XValidator does, but there is
one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item
arrive to my validator.
Bye
Andrea
NS> Well, first of all, the web.config file is an XML file, so you
won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a
.VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or
sub,
NS> or you could add the function or sub to the Global class (located
in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it
might
NS> help me and anyone else figure out what you are looking for.
(NOTE:
NS> When posting a Reply, it is good to include the previous message
so
NS> that people can see anything you have already tried, mentioned,
as
NS> well as what the original problem was)
message
NS> NS>
Well, I need to use it in the web.config file, not in an asp page,
so I suppose that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,243
Latest member
Weeb3PRAgency

Latest Threads

Top