1

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.

enter image description here

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.

enter image description here

Please help me through.

2
  • Does it work with only one .matches in the password validation? Commented Mar 4, 2016 at 19:53
  • No , it does not. It still shows an error in LastName. When I comment that code block for LastName out from FluentValidator , it shows error for FirstName. Commented Mar 4, 2016 at 21:09

1 Answer 1

1

This issue might be due to multiple number of value is set for same key

As ValidatonMessage is a dictionary type.

Remove all the decoration/data annotation on the viewmodel and use the fluentvalidator,its going to work.

Please refer to

Validation type names in unobtrusive client validation rules must be unique

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.