Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net mvc4 validation check if property values are the same
    primarykey
    data
    text
    <p>I have tried to compare password and confirm password on a form using validation attributes but when I submit the form with different values on the two fields an error is not displayed next to the confirm password field. </p> <p>I have tried the method for MVC3 validation on this <a href="https://stackoverflow.com/questions/8473671/mvc3-validation-check-if-property-values-differ">question</a> but it didn't help.</p> <p>Here is the source code:</p> <pre><code>//Model using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Globalization; using System.Web.Security; using System.Web.Mvc; using mvcdemo.Validation; namespace mvcdemo.Models { public class User:IValidatableObject { public int userid { get; set; } [Required] [Remote("Username", "User",ErrorMessage = "The username is not allowed.")] //remote server validation asynchronous public string username { get; set; } public string password { get; set; } [PasswordCreationRule("password",ErrorMessage ="Password and Confirm Password have to be the same")] public string ConfirmPassword { get; set; } public string email { get; set; } public int roleid { get; set; } [Required] [Display(Name = "Password at first logon status")] public string passwordatfirstlogonstatus { get; set; } } public class UserDBContext : DbContext { public DbSet&lt;User&gt; users { get; set; } } public class administrator : User { } } // Validation class using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel.DataAnnotations; using System.Reflection; namespace mvcdemo.Validation { [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] public class PasswordCreationRuleAttribute:ValidationAttribute { private const string defaultErrorMessage = "{0} cannot be different same as {1}."; private string otherProperty; public PasswordCreationRuleAttribute(string otherProperty): base(defaultErrorMessage) { if(string.IsNullOrEmpty(otherProperty)) { throw new ArgumentNullException("otherProperty"); } this.otherProperty = otherProperty; } public override string FormatErrorMessage(string name) { return string.Format(ErrorMessageString, name, otherProperty); } protected override ValidationResult IsValid(object value,ValidationContext validationContext) { if (value != null) { PropertyInfo otherPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(otherProperty); if (otherPropertyInfo == null) { return new ValidationResult(string.Format("Property '{0}' is undefined.", otherProperty)); } var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); if (otherPropertyValue != null) { if (value.Equals(otherPropertyValue)) { return ValidationResult.Success; } else { return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } } } return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload