7

For instance, I have an Employee view model. When creating an employee, I want to validate the username to make sure it doesn't exist.

public class EmployeeViewModel
{
    [ScaffoldColumn(false)]
    public int EmployeeId { get; set; }

    [ValidateDuplicate(ErrorMessage = "That username already exists")]
    [Required(ErrorMessage = "Username is required")]
    [DisplayName("Username")]
    public string Username { get; set; }
}

And then have my ValidateDuplicate function somewhere with the code to check for a duplicate.

Is this possible?

4 Answers 4

15

I would suggest looking at remote validation. The example even matches your case.

Basically, add the remote attribute to your viewmodel property that points to a controller action

[Remote("IsUserExists", "Account", ErrorMessage = "Can't add what already exists!")]
[Required(ErrorMessage = "Username is required")]
[DisplayName("Username")]
public string Username { get; set; }

which does your work

public ActionResult IsUserExists(string userName) 
{
 if (!UserService.UserNameExists(userName) || (CurrentUser.UserName == userName))
 {
      return "Ok.";
 }
}
Sign up to request clarification or add additional context in comments.

1 Comment

David Hayden migrated his blog to elsewhere and appears he did not port back his old blog posts (or couldn't). That being said, here's a link directly to Microsoft's documentation: msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx
2

Yes, it's possible. You'll need to write your own validation attribute.

Comments

2

You can write your own custom validation as explained here. I've modified the code to add validation in the model as I prefer rails active record's validation style in the model.

public class EmployeeViewModel
{

    [CustomValidation(typeof(EmployeeViewModel), "ValidateDuplicate")]
    [Required(ErrorMessage = "Username is required")]
    [DisplayName("Username")]
    public string Username { get; set; }

    public static ValidationResult ValidateDuplicate(string username)
    {
      bool isValid;

      using(var db = new YourContextName) {
        if(db.EmployeeViewModel.Where(e => e.Username.Equals(username)).Count() > 0)
       {
          isValid = false;
       } else {
          isValid = true;
       }
      }

      if (isValid)
      {
        return ValidationResult.Success;
      }
      else
      {
        return new ValidationResult("Username already exists");
      }

    }
}

Comments

0

You can do that by extending the Attribute class calling it ValidateDuplicate. I'd avoid doing it this way because its just another place where you potentially have to make a call to a database.

Comments

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.