4

I have the following model

public partial class OwnerData
{
    public string firstname { get; set; }
    public string lastname { get; set; }

    [Required]
    [StringLength(60)]
    [EmailAddress]
    [MailNotExists] //this is a custom function
    public string email { get; set; }
}

the custom function MailNotExists is the following

namespace MyApp.CustomDataAnnotations
{
    public class MailNotExists : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            DbContext db = new DbContext ();

            var user = db.Users.FirstOrDefault(u => u.email == (string)value);

            if (user == null)
                return ValidationResult.Success;
            else
                return new ValidationResult("Mail already exists");
        }
    }
}

The model is used into the view to give the user the possibility to change his mail, trough

 @Html.EditorForModel();

The html output returns into the editor also the already existing mail, if you want to change it the system first check if the mail is already present in the db trough the validator [MailNotExists] and everything is ok. The only problem is that if the user don't want to change the mail but only the firstname or lastname and then submit the form the validator returns an error because the mail already exists.

Any solution on how to by-pass the validator only in that particular case?

1 Answer 1

2

Well its a bit specific since i dont see primary key, but i would do it next way add primary key to owner data

public partial class OwnerData
        {
            public int Id { get; set; }
            public string firstname { get; set; }
            public string lastname { get; set; }

            [Required]
            [StringLength(60)]
            [EmailAddress]
            [MailNotExists] //this is a custom function
            public string email { get; set; }
        }

And modify your attribute

  protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var owner = validationContext.ObjectInstance as OwnerData;
            if(owner == null) return new ValidationResult("Model is empty");
            DbContext db = new DbContext();
            var user = db.Users.FirstOrDefault(u => u.email == (string) value && u.id != owner.Id);

            if (user == null)
                return ValidationResult.Success;
            else
                return new ValidationResult("Mail already exists");
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes sorry, i forgot the insert the PK in my example. In fact in my code the model contains a lot of properties, but i just cleaned it up to be more syinthetic. Anyway, your solution works perfect fine. Thank you!

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.