7

I want to make my own validation class (i have a lot of validation methods in JS that i want to translate into C# to use with MVC models) that works exactly like data annotations do, validating in client and server side: [DataType(MyDataType)] or like a Validation DataAnnotation Attribute like this: [MyDataTypeValidation]

i don't know wich option is better to make my validation "library"

In example i have my class FigurasDA and i want to make my custom validation to the attribute nombre.

namespace MonitoreoIntegrado.Models
{
    [MetadataType(typeof(FigurasDA))]
    public partial class Figuras
{
}

public class FigurasDA
{
    [DataType(MyDataType)]
    //or
    [MyDataTypeValidation]
    public string nombre { get; set; }
}
}

so in this case, i want to validate that the string matches the regexp @"^[\w\s\.\-_]+$" and shows a error message like this "Solo se permite letras, numeros y puntuaciones(- _ .)" if don't. (this is my "Alfanumerico" datatype).

Can you give me an example where to put my validation class and what code write inside?.

2
  • Have you tried the RegularExpressionAttribute ex:[RegularExpression(@"pattern", ErrorMessage="")]? Or you can extend one of the ValidationAttribute classes. Commented Jun 13, 2014 at 18:27
  • I want to use this regexp in a lot of classes. so i need a class i guess Commented Jun 13, 2014 at 18:33

1 Answer 1

11

Actually it's easy... You just have to inherit you custom validation attribute from ValidationAttribute class and provide your own IsValid logic. For example:

public class MyDataTypeValidationAttribute : ValidationAttribute
{
    private Regex _regex = new Regex(@"^[\w\s.-_]+$");          

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {               
        if (_regex.IsMatch(value.ToString()))
        {
            return ValidationResult.Success;
        }

        return new ValidationResult("Solo se permite letras, numeros y puntuaciones(- _ .)" );
    }
}

and in your view model you can use:

public class FigurasDA
{    
    [MyDataTypeValidation]
    public string nombre { get; set; }
}

You can save this validation attribute, for example in Attributes folder in your MVC project:

Step 1

Step 2

Step 3

Sign up to request clarification or add additional context in comments.

4 Comments

@Clamari, wherever you want in your solution/project. You can create folder named Attributes and put there all your custom validation logic.
In a DataValidation.css file inside my project root IE?
@Clamari, css is cascade style sheet file, it contains html page styles. I provided some screen shots for example, see my edits.
Sorry, was a typo. DataValidation.cs it was just an extra "s" haha thanks a lot :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.