Trying to validate data types

Topics: Validation Application Block
May 15, 2007 at 6:53 PM
Hello, I am trying to validate data types, I tried typevalidator and regexvalidor

the typevalidator has not a message template property.

RegexValidator(@"\d", MessageTemplate = "Invalid number", Ruleset = "RuleSetA")
public int IdPrioridad
{
get { return _idPrioridad; }
set { _idPrioridad = value; }
}

I want the message Invalid number to appear on the page, but it appears.

The supplied value could not be converted to the target property type.


How can I accomplish this with the Typevalidator??


Also the notnullvalidator has not Messaage template, so how can I show a differente message if the user does not enter a value.

RegexValidator(@"\d", MessageTemplate = "Invalid number", Ruleset = "RuleSetA")
NotNullValidator
public int IdPrioridad
{
get { return _idPrioridad; }
set { _idPrioridad = value; }
}
May 16, 2007 at 3:33 PM
The RegexValidator validates strings not integers, so using the RegexValidator on a property of type integer will not work.

An integer (value type) can also never be null, so that is not going to work like you expected either I don't think.

You have the idea, just the wrong validators. Maybe you want a RangeValidator to specify the integer needs to be between 1 and 20 like:

 
public class MyClass {
 
    private int _idPrioridad;
 
    [RangeValidator<int>(1,20),MessageTemplate="Invalid number.", Ruleset="RulesSetA"]
    public int IdPrioridad
    {
        get { return _idPrioridad; }
        set { _idPrioridad = value; }
    }
 
}

Then you can validate the class:

MyClass myClass = new MyClass();
myClass.IdPrioridad = 25; // Out Of Bounds
ValidationResults results = Validation.Validate(myClass);
 
// ....

I typed that up quickly, but hopefully it gives you the idea. The QuickStarts that come with Enterprise Library 3.0 are a great source of information.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
May 16, 2007 at 4:23 PM
Thank you David.

What I am trying to do is to create in a single property all the validations I need for an int.
- The field cannot be blank, it has to show a differente message if its blank.
- The field cannnot be a letter, it has to show a different message if its blank
-The field has to be a number between 0 and 100, otherwise show another message.

I have this.
IgnoreNulls
TypeConversionValidator(typeof(int), Ruleset="RuleSetA", MessageTemplate="Prioridad debe ser númerico")
RangeValidator(0, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive, MessageTemplate = "Campo no valido, debe ser entre 0 y 100", Ruleset = "RuleSetA")
public int IdPrioridad
{
get { return _idPrioridad; }
set { _idPrioridad = value; }

the range validator works fine if I type a number on the texbox, but the ignorenulls and type conversion are not working.

I need the 3 validators.


May 16, 2007 at 4:25 PM
I also tried this.

ValidatorComposition(CompositionType.And)
TypeConversionValidator(typeof(int), Ruleset="RuleSetA", MessageTemplate="Prioridad debe ser númerico")
RangeValidator(0, RangeBoundaryType.Inclusive, 100, RangeBoundaryType.Inclusive, MessageTemplate = "Campo no valido, debe ser entre 0 y 100", Ruleset = "RuleSetA")
public int IdPrioridad
{
get { return _idPrioridad; }
set { _idPrioridad = value; }
}

but If I type for example an X on the Idprioridad field, I got this message on the propertyproxyvalidator.
The supplied value could not be converted to the target property type.


The code behind is this.
BLPerfil objBlPerfil = new BLPerfil();
Perfil objPerfil = new Perfil();
objPerfil.IdPrioridad = Convert.ToInt32(TextBox2.Text);
objPerfil.DSPerfil = TextBox1.Text;
objBlPerfil.Ingresar(objPerfil);
GridView1.DataBind();