I have been facing this issue a while and have tried all sorts of facets. Nothing worked out till now. This is what I have. I have a Student.cs domain model class.
[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; }
}
And a StudentViewModel Class :
//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; }
}
The Department Model class looks like :
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; }
}
The FluentValidation Rules look like :
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 . $ ~ &");
}
}
The validation keeps on breaking saying
Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: regex
at Password
field.
The View looks like :
@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>
Update
If I put these lines at Application_Start()
//var fluentValidationModelValidatorProvider = new FluentValidationModelValidatorProvider(new AttributedValidatorFactory());
//ModelValidatorProviders.Providers.Add(fluentValidationModelValidatorProvider);
//DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
//fluentValidationModelValidatorProvider.AddImplicitRequiredValidator = false;
Then the validation breaks at LastName
Field.
Please help me through.
答案 0 :(得分:1)
此问题可能是由于为同一个键设置了多个值
由于ValidatonMessage是字典类型。
删除viewmodel上的所有装饰/数据注释并使用fluentvalidator,它将起作用。
请参阅
Validation type names in unobtrusive client validation rules must be unique