using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; using System.Web.Mvc; using Sleis.Infrastructure; using Sleis.Utility; namespace Sleis.Models { public class ResetPasswordModel : BaseModel, IValidatableObject { const string NOT_SAME_PASS_ERR = "The confirmed value does not match the new password."; static readonly Regex _regEx; static readonly string _errMessage; static ResetPasswordModel() { IPropertyPlaceholder PropProvider = SpringServiceProvider.GetService(); var passwordRegEx = PropProvider.Get("valid.pass.regex"); ArgumentValidationUtility.ThrowOnEmpty(passwordRegEx, "Regular Expression for passwords not defined: 'valid.pass.regex'"); _errMessage = PropProvider.Get("valid.pass.error"); ArgumentValidationUtility.ThrowOnEmpty(_errMessage, "Password format error not defined: 'valid.pass.error'"); _regEx = new Regex(passwordRegEx, RegexOptions.Compiled); } [Required (ErrorMessage=Constants.RequiredErrorMessage)] public virtual string Password1 { get; set; } [Required(ErrorMessage = Constants.RequiredErrorMessage), Compare("Password1", ErrorMessage = NOT_SAME_PASS_ERR)] public virtual string Password2 { get; set; } public virtual IEnumerable Validate(ValidationContext ctx) { List errrs = new List(); if (String.IsNullOrEmpty(Password1) || String.IsNullOrEmpty(Password2) || Password1 != Password2) { errrs.Add(new ValidationResult(NOT_SAME_PASS_ERR, new string[] { "Password1", "Password2" })); } if (!_regEx.IsMatch(Password1)) { errrs.Add(new ValidationResult(_errMessage, new string[] { "Password1", "Password2" })); } return errrs; } } }