views:

27

answers:

2

Can the built in ASP MVC validation be made to behave differently for different actions of a same controller ? For example I have a user controller and it has actions like create, edit and other actions. So in model user the attribute Username is being validated for its uniqueness. If there is an user present with the same username, it throws and error username already present. So using the same validator for edit action throws an error "username already present" while editing an user. Can anybody tell me if there is a way to do solve this problem? I am pasting my validator code for reference.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;  

    namespace Models
    {
        [MetadataType(typeof(AdmiUserMetadata))]
        public partial class AdminUser
        {
            public class AdmiUserMetadata
            {
                [Required(ErrorMessage = "Required Field")]
                public string Id { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
                [Username(ErrorMessage = "Username already taken")]
                public string Username { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
                public string Password { get; set; }

                [Required(ErrorMessage = "Required Field")]
                public string Name { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage ="Invalid E-mail ID")]
                public string Email { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("(Active|Disabled)", ErrorMessage = "Select the status of User")]
                public string Status { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("^[1-9]", ErrorMessage = "Select the group of User")]
                public string Group { get; set; }
            }
        }

        public class UsernameAttribute : ValidationAttribute
        {
            IUserRepository _repository = new UserRepository();
            public override bool IsValid(object value)
            {
                if (value == null)  
                    return true;
                if (_repository.IsUsernamePresent((string)value))
                {
                    return false;
                }
                return true;
            }
        }
    }
A: 

I don't see how this can be done if a class has attributes that determines validation. This obviously works for most projects, but for me this is also not working out.

If you need to attach different sets of validation rules check out http://fluentvalidation.codeplex.com/. I tried it and liked it.

It doesn't handle client validation. I dropped that because I have ajax calls in most parts and that feels a bit like client validation.

Malcolm Frexner
Thanks for the link ... will check it out ...
Johnson
A: 

What you are validating is a business rule.

No two users can have the same username.

I would have a User service that enforces this rule on creation/edit. Attributes are best suited for input validation. (eg Is the integer non-negative? A valid email address? etc)

Ryan
You maybe right ... I think I got the whole point of attribute validation wrong... anyway thanks for the heads up ...
Johnson