Asp MVC 2 针对现有电子邮件地址进行远程验证,但编辑怎么样?

发布于 2024-10-16 05:01:20 字数 8988 浏览 4 评论 0原文

长期读者,第一次海报。

我正在使用 ASP MVC 2 和远程验证。我有一个名为 PersonVM 的模型,AddPersonVM 和 EditPersonVM 继承自该模型。这样我就可以使用一个用户控件来处理两个子类模型之间相同的大部分 UI。

/// <summary>
/// This class handles adding and editing people.
/// </summary>
[MustHaveAtLeastOneOf("HomePhoneNumber", "CellPhoneNumber", ErrorMessage="At least one of the phone numbers must be filled in.")]
public abstract class PersonVM : WebViewModel
{

    #region Reference (for Binding)

    /// <summary>
    /// Provides a list of person types, e.g. Teacher, Student, Parent.
    /// </summary>
    public IEnumerable<PersonType> ListOfPersonTypes {
        get {
            if (_listOfPersonTypes == null) {
                _listOfPersonTypes = Data.PersonTypes
                    .Where(pt => pt.OrganizationID == ThisOrganization.ID || pt.OrganizationID == null)
                    .OrderBy(pt => pt.Name).ToArray();
            }
            return _listOfPersonTypes;
        }
    }
    private IEnumerable<PersonType> _listOfPersonTypes = null;

    /// <summary>
    /// Provides a list of schools the person can belong to.
    /// </summary>
    public IEnumerable<School> ListOfSchools {
        get {
            if (_listOfSchools == null) {
                _listOfSchools = Data.Schools
                    .Where(s =>
                        (s.OrganizationID == ThisOrganization.ID || s.OrganizationID == null) // TODO
                        && s.Deleted == false
                        )
                    .OrderBy(s => s.Name);
            }
            return _listOfSchools;
        }
    }
    private IEnumerable<School> _listOfSchools = null;

    /// <summary>
    /// Provides a list of contact types, e.g. Home phone, email.
    /// </summary>
    public IEnumerable<ContactType> ListOfContactTypes {
        get {
            if (_listOfContactTypes == null) {
                _listOfContactTypes = Data.ContactTypes
                    .OrderBy(ct => ct.Name);
            }
            return _listOfContactTypes;
        }
    }
    private IEnumerable<ContactType> _listOfContactTypes = null;

    /// <summary>
    /// Provides a list of genders.
    /// </summary>
    public IEnumerable<string> Genders {
        get {
            return new string[] { null, "Male", "Female" };
        }
    }

    /// <summary>
    /// Returns a list of US states.
    /// </summary>
    public List<StaticData.USState> ListOfUSStates {
        get {
            return StaticData.USStates;
        }
    }

    #endregion

    /// <summary>
    /// Represents the current person being edited.
    /// </summary>
    public Person CurrentPerson { get; set; }

    #region Abstracted Required Fields
    /*
     * This is done, since address information, DOB
     * are not required on the panel, but are required fields here. 
     * I tried implementing an interface called IPersonAddressRequired 
     * with this information in it, but it didn't work. Additionally,
     * only one instance of [MetadataType] is allowed on a class,
     * so that didn't work either.
     */

    [Required]
    [StringLength(100)]
    public string Address {
        get { return CurrentPerson.Address; }
        set { CurrentPerson.Address = value; }
    }

    [StringLength(100)]
    public string Address2 {
        get { return CurrentPerson.Address2; }
        set { CurrentPerson.Address2 = value; }
    }

    [Required]
    [StringLength(50)]
    public string City {
        get { return CurrentPerson.City; }
        set { CurrentPerson.City = value; }
    }

    [Required]
    [StringLength(50)]
    public string State {
        get { return CurrentPerson.State; }
        set { CurrentPerson.State = value; }
    }

    [Required]
    [StringLength(50)]
    public string Zip {
        get { return CurrentPerson.Zip; }
        set { CurrentPerson.Zip = value; }
    }

    [DataType(DataType.Date)]
    [Required]
    public DateTime? DOB {
        get { return CurrentPerson.DOB; }
        set { CurrentPerson.DOB = value; }
    }

    [Required]
    public string Gender {
        get { return CurrentPerson.Gender; }
        set { CurrentPerson.Gender = value; }
    }

    #endregion

    #region Abstracted Contact Methods

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Home Phone Number")]
    [DisplayName("Home Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string HomePhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Cell Phone Number")]
    [DisplayName("Cell Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string CellPhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the email address contact record.
    /// </summary>
    [Display(Name = "Email Address")]
    [DisplayName("Email Address")]
    [Required]
    [UniqueEmailAddress(ErrorMessage="The email address was already found in our system, please sign in or use another email.")]
    [Email(ErrorMessage="The email address specified isn't an email address.")]
    public string EmailAddress { get; set; }

    #endregion

    //////////////////////////////

    /// <summary>
    /// Sets the picture to be uploaded.
    /// </summary>
    public byte[] PictureData { get; set; }

    /// <summary>
    /// One of 'keep', 'remove', 'replace'.
    /// </summary>
    public string PictureAction { get; set; }

}

我有 2 个继承此模型的模型,AddPersonVM 和 EditPersonVM。每个人处理提交更改的方式都略有不同。

[UniqueEmailAddress] 是我的 RemoteAttribute,用于检查现有电子邮件地址以及我遇到的问题。

这在添加某人时效果很好,但在编辑现有人员时,会员系统中已存储的电子邮件地址已存在,因此验证失败。我想要做的是存储属性值的原始值,并将其从 IsValid 检查中排除。

我的代码:

/// <summary>
/// Checks to be sure that an entered email address is unique.
/// </summary>
public class UniqueEmailAddressAttribute : RemoteAttribute
{

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { }

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}

我想要什么:

/// /// 检查以确保输入的电子邮件地址是唯一的。 /// 公共类 UniqueEmailAddressAttribute :RemoteAttribute {

字符串 OriginalValue = null;

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { 
OriginalValue = value Found using reflection or something;
}

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

if ((string)value == OriginalValue)
return true;

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}

<代码> 有什么想法吗?截至目前,我可以将 EmailAddress 属性单独添加到 AddPersonVM 和 EditPersonVM,将该字段从我的用户控件视图中取出并添加到两个视图,然后删除“编辑”上的属性,但这将阻止任何人编辑其帐户以拥有他们想要的任何电子邮件地址。我不在乎是否需要反射来读取原始值,这很酷。

提前致谢! - 德雷克

Long-time reader, first time poster.

I'm using ASP MVC 2 and remote validation. I have a model called PersonVM, which AddPersonVM and EditPersonVM inherit from. This is so I can use one usercontrol to handle most of the UI that is the same between both subclassed models.

/// <summary>
/// This class handles adding and editing people.
/// </summary>
[MustHaveAtLeastOneOf("HomePhoneNumber", "CellPhoneNumber", ErrorMessage="At least one of the phone numbers must be filled in.")]
public abstract class PersonVM : WebViewModel
{

    #region Reference (for Binding)

    /// <summary>
    /// Provides a list of person types, e.g. Teacher, Student, Parent.
    /// </summary>
    public IEnumerable<PersonType> ListOfPersonTypes {
        get {
            if (_listOfPersonTypes == null) {
                _listOfPersonTypes = Data.PersonTypes
                    .Where(pt => pt.OrganizationID == ThisOrganization.ID || pt.OrganizationID == null)
                    .OrderBy(pt => pt.Name).ToArray();
            }
            return _listOfPersonTypes;
        }
    }
    private IEnumerable<PersonType> _listOfPersonTypes = null;

    /// <summary>
    /// Provides a list of schools the person can belong to.
    /// </summary>
    public IEnumerable<School> ListOfSchools {
        get {
            if (_listOfSchools == null) {
                _listOfSchools = Data.Schools
                    .Where(s =>
                        (s.OrganizationID == ThisOrganization.ID || s.OrganizationID == null) // TODO
                        && s.Deleted == false
                        )
                    .OrderBy(s => s.Name);
            }
            return _listOfSchools;
        }
    }
    private IEnumerable<School> _listOfSchools = null;

    /// <summary>
    /// Provides a list of contact types, e.g. Home phone, email.
    /// </summary>
    public IEnumerable<ContactType> ListOfContactTypes {
        get {
            if (_listOfContactTypes == null) {
                _listOfContactTypes = Data.ContactTypes
                    .OrderBy(ct => ct.Name);
            }
            return _listOfContactTypes;
        }
    }
    private IEnumerable<ContactType> _listOfContactTypes = null;

    /// <summary>
    /// Provides a list of genders.
    /// </summary>
    public IEnumerable<string> Genders {
        get {
            return new string[] { null, "Male", "Female" };
        }
    }

    /// <summary>
    /// Returns a list of US states.
    /// </summary>
    public List<StaticData.USState> ListOfUSStates {
        get {
            return StaticData.USStates;
        }
    }

    #endregion

    /// <summary>
    /// Represents the current person being edited.
    /// </summary>
    public Person CurrentPerson { get; set; }

    #region Abstracted Required Fields
    /*
     * This is done, since address information, DOB
     * are not required on the panel, but are required fields here. 
     * I tried implementing an interface called IPersonAddressRequired 
     * with this information in it, but it didn't work. Additionally,
     * only one instance of [MetadataType] is allowed on a class,
     * so that didn't work either.
     */

    [Required]
    [StringLength(100)]
    public string Address {
        get { return CurrentPerson.Address; }
        set { CurrentPerson.Address = value; }
    }

    [StringLength(100)]
    public string Address2 {
        get { return CurrentPerson.Address2; }
        set { CurrentPerson.Address2 = value; }
    }

    [Required]
    [StringLength(50)]
    public string City {
        get { return CurrentPerson.City; }
        set { CurrentPerson.City = value; }
    }

    [Required]
    [StringLength(50)]
    public string State {
        get { return CurrentPerson.State; }
        set { CurrentPerson.State = value; }
    }

    [Required]
    [StringLength(50)]
    public string Zip {
        get { return CurrentPerson.Zip; }
        set { CurrentPerson.Zip = value; }
    }

    [DataType(DataType.Date)]
    [Required]
    public DateTime? DOB {
        get { return CurrentPerson.DOB; }
        set { CurrentPerson.DOB = value; }
    }

    [Required]
    public string Gender {
        get { return CurrentPerson.Gender; }
        set { CurrentPerson.Gender = value; }
    }

    #endregion

    #region Abstracted Contact Methods

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Home Phone Number")]
    [DisplayName("Home Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string HomePhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Cell Phone Number")]
    [DisplayName("Cell Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string CellPhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the email address contact record.
    /// </summary>
    [Display(Name = "Email Address")]
    [DisplayName("Email Address")]
    [Required]
    [UniqueEmailAddress(ErrorMessage="The email address was already found in our system, please sign in or use another email.")]
    [Email(ErrorMessage="The email address specified isn't an email address.")]
    public string EmailAddress { get; set; }

    #endregion

    //////////////////////////////

    /// <summary>
    /// Sets the picture to be uploaded.
    /// </summary>
    public byte[] PictureData { get; set; }

    /// <summary>
    /// One of 'keep', 'remove', 'replace'.
    /// </summary>
    public string PictureAction { get; set; }

}

I have 2 models that inherit from this model, AddPersonVM and EditPersonVM. Each implements how they handle submitting changes a little differently.

[UniqueEmailAddress] is my RemoteAttribute that checks for an existing email address, and what I have issue with.

This works fine on adding someone, but when editing an existing person, the email address already stored exists in the membership system, so it fails validation. What I would like to do is store the original value of the property's value, and exclude that from what IsValid checks.

My code:

/// <summary>
/// Checks to be sure that an entered email address is unique.
/// </summary>
public class UniqueEmailAddressAttribute : RemoteAttribute
{

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { }

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}

What I would like to have:


///
/// Checks to be sure that an entered email address is unique.
///
public class UniqueEmailAddressAttribute : RemoteAttribute
{

string OriginalValue = null;

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { 
OriginalValue = value Found using reflection or something;
}

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

if ((string)value == OriginalValue)
return true;

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}


Any ideas? As of now, I could add the EmailAddress property individually to both AddPersonVM and EditPersonVM, take the field out of my usercontrol view and add to both views, and remove the attribute on Edit, but then that would stop noone from editing their account to have whatever email address they want. I don't care if reflection is required to read the original value, that's cool.

Thanks in advance! - Derreck

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-10-23 05:01:20

我认为你想添加具有唯一电子邮件地址的人,如果任何用户尝试更改他/她正在使用的电子邮件地址,你想检查他是否更改的唯一性,但如果他没有更改,你也不想检查唯一性更改他的电子邮件地址。

所以我的建议是向 IsEmailAddressInUse (string email, bool IsEdit) 添加一个属性。
如果 IsEdit 为 true,则检查用户登录信息中的电子邮件地址。如果相同则返回 true 如果不检查唯一性。

I think you want to add person with unique email address and if any user try to change his/her email address which is in use you want to check uniquness if he change, but you also don't want to check uniquness if he does not change his email address.

So my suggestion is add one more property to IsEmailAddressInUse (string email, bool IsEdit).
If IsEdit is true then check email address from user loggedin info. If same return true if not check uniquness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文