1
的流暢的驗證休息時間我一直在面對這個問題,並嘗試了各種各樣的方面。到現在爲止沒有任何工作。這是我的。我有一個Student.cs域模型類。ASP .Net MVC應用程序
[FluentValidation.Attributes.Validator(typeof(StudentViewModelValidator))]
public class Student
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentId { get; set; }
public String LastName { get; set; }
public String FirstName { get; set; }
public String UserName { get; set; }
public String Password { get; set; }
[ForeignKey("Department")]
public int DepartmentID { get; set; }
//Department Navigational Property
public Department Department { get; set; }
}
並有StudentViewModel類:
//View Model
public class StudentViewModel
{
public Student Student { get; set; }
[Display(Name = "StudentName")]
public String StudentFullName
{
get
{
return String.Format("{0} {1}", Student.FirstName, Student.LastName);
}
}
[DataType(DataType.Password)]
public String Password { get; set; }
[DataType(DataType.Password)]
[Compare("Password",ErrorMessage="Passwords do not match")]
[Display(Name="Confirm Password")]
public String C_Password { get; set; }
[Display(Name = "Department Name")]
public String DepartmentName { get; set; }
}
部Model類的樣子:
public class Department
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int DepartmentID { get; set; }
//[Required(ErrorMessage="Department Name cannot be empty")]
public String DepartmentName { get; set; }
//[Required(ErrorMessage = "Department Code cannot be empty")]
public String DepartmentCode { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
的FluentValidation規則是這樣的:
public class StudentViewModelValidator : AbstractValidator<Student>
{
public StudentViewModelValidator()
{
RuleFor(m => m.FirstName)
.NotEmpty().WithMessage("First Name is required.")
.Matches(@"^\D*$").WithMessage("Special characters and numbers are not allowed in First Name");
RuleFor(m => m.LastName)
.NotEmpty().WithMessage("Last Name is required.")
.Matches(@"^\D*$").WithMessage("Special characters and numbers are not allowed in Last Name");
RuleFor(m => m.UserName)
.NotEmpty().WithMessage("User Name is required.")
.Matches(@"^a-zA-Z0-9\.\$").WithMessage("Only . and $ are allowed special characters in a user name");
RuleFor(m => m.Password)
.NotEmpty().WithMessage("Password is required.")
.Length(4, 10).WithMessage("Password should be 4 to 10 characters long")
.Matches(@".*\d{2}").WithMessage("Password should contain at least 2 digits")
.Matches(@".*a-zA-Z").WithMessage("Password should contain at least one albhabet")
.Matches(@"[\.\$\~\&]").WithMessage("Allowed special characters in a password are . $ ~ &");
}
}
驗證不斷破壞稱爲
不引人注意的客戶端驗證規則中的驗證類型名稱必須是唯一的。以下驗證類型不止一次出現:正則表達式
at Password
field。
觀的樣子:
@model MvcApplication4.Models.Student
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend><strong>Create a record</strong></legend>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.DepartmentID)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.DepartmentID)
@Html.ValidationMessageFor(model => model.DepartmentID)
</div>
<p>
<input type="submit" class="createDetails" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
更新
如果我把這些線在Application_Start()
//var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new AttributedValidatorFactory());
//ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
//DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
//fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;
然後驗證處斷裂LastName
字段。
請通過幫助我。
是否只有一個在密碼驗證.matches工作? – Duston
不,它沒有。它仍然在LastName中顯示錯誤。當我從FluentValidator註釋到LastName的代碼塊時,它顯示FirstName的錯誤。 – StrugglingCoder