Итак, у меня есть модель «Пользователь», в которой много полей, но следующие из них:
public int Id {get;set;}
public string Username { get; set; }
public string Pwd { get; set; }
и у меня есть модель представления, которая проверяет пароль, который я использую на другом контроллере:
public class ConfirmPassword : IValidatableObject
{
[Required]
public string Password { get; set; }
[Required(ErrorMessage="Confirm Password field is required.")]
public string ConfirmPwd { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
string regex1 = @"^.{8,10}$"; // 8 - 10 characters
Match requirement1 = Regex.Match(Password, regex1);
if (Password != ConfirmPwd)
yield return new ValidationResult("Password and Confirm Password is not identical.");
if (!requirement1.Success)
yield return new ValidationResult("Password must be between 8 and 10 characters.");
}
}
есть ли способ, где я могу подключить модель представления к моей модели? или просто скопировать и вставить коды мой единственный вариант? Я не могу скопировать и вставить, так как код должен иметь IValidateObject, который испортит всю сущность пользователя, поскольку происходит множество фоновых проверок. Мне просто нужно проверять пароли всякий раз, когда профиль редактируется/создается.
РЕДАКТИРОВАТЬ: извините, если все запутались. в основном, у меня есть несколько проверок, которые мне нужны для подтверждения паролей, которые не могут быть обработаны аннотациями данных, поэтому модель просмотра подтверждения пароля. эту проверку я хочу применить к модели пользователя, но без добавления в нее поля «ConfirmPassword». так как мне не нужно другое поле в базе данных. Мой вопрос заключается в том, как заставить проверки, которые у меня есть из подтверждения пароля, срабатывать всякий раз, когда представление POST и поле пароля не соответствуют его требованиям?
namespace
{
public class User
{
public int Id {get;set;}
public string Username { get; set; }
public string Pwd { get; set; }
}
}
namespace
{
public class ConfirmPassword : IValidatableObject
{
[Required]
public string Password { get; set; }
[Required(ErrorMessage="Confirm Password field is required.")]
public string ConfirmPwd { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
string regex1 = @"^.{8,10}$"; // 8 - 10 characters
string regex2 = @"(?:.*?[A-Z]){1}"; // 1 uppercase
string regex3 = ""; // 1 lowercase
string regex4 = ""; // 1 numeric
Match requirement1 = Regex.Match(Password, regex1);
Match requirement2 = Regex.Match(Password, regex2);
Match requirement3 = Regex.Match(Password, regex3);
Match requirement4 = Regex.Match(Password, regex4);
if (Password != ConfirmPwd)
yield return new ValidationResult("Password and Confirm Password is not identical.");
if (!requirement1.Success)
yield return new ValidationResult("Password must be between 8 and 10 characters.");
if (!requirement2.Success)
yield return new ValidationResult("Password must contain at least 1 uppercase letter.");
if (!requirement3.Success)
yield return new ValidationResult("Password must contain at least 1 lowercase letter.");
if (!requirement4.Success)
yield return new ValidationResult("Password must contain at least 1 numeric character.");
}
}
}
StringLengthAttribute
- person Daniel A. White   schedule 28.06.2011