У меня есть ViewModel, который содержит дату, час и минуты в виде строки, у каждого из них есть текстовое поле для ввода значений в них, что я тогда хочу, это пользовательский валидатор, который гарантирует, что если значение вводится в один из поля, остальные должны быть заполнены, иначе они могут быть пустыми, все или ничего!
Я сделал небольшой тестовый проект здесь, но он не работает так, как должен, я следовал http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2, но я думаю, что мои проблемы на стороне клиента, так как я не вижу значения в параметрах, если я это делаю точка останова в хроме.
Вид:
@model validationproject.Models.DateTimeViewModel
@{
ViewBag.Title = "ViewPage1";
}
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<script src="../../Scripts/Validators.js" type="text/javascript"></script>
<h2>ViewPage1</h2>
@using (Ajax.BeginForm("CreateDateTime", "Home", new {}, new AjaxOptions {HttpMethod = "Post"}))
{
<span>
@Html.TextBoxFor(model => model.Date, new {@Id = "date"})
@Html.TextBoxFor(model => model.Hour, new {@Id = "hour"})
@Html.TextBoxFor(model => model.Minute, new {@Id = "minute"})
@Html.ValidationMessageFor(model => model.Date)
@Html.ValidationMessageFor(model => model.Hour)
@Html.ValidationMessageFor(model => model.Minute)
</span>
}
Файл валидатора JQuery
$.validator.unobtrusive.adapters.add(
'fulldaterequired',
['part1', 'part2'],
function (options) {
options.rules['fulldaterequiredcheck'] = options.params;
options.messages['fulldaterequiredcheck'] = options.message;
}
);
$.validator.addMethod(
'fulldaterequiredcheck',
function (value, element, params) {
if (!value && (params['otherproperty1'] || params['otherproperty2'])) {
return false;
}
return true;
}
);
Моя модель представления
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace validationproject.Models
{
public class DateTimeViewModel
{
[Display(Name = "Date")]
[FullDateRequired(("Hour"), ("Minute"))]
public string Date { get; set; }
[Display(Name = "Hour")]
[FullDateRequired(("Minute"), ("Date"))]
public string Hour { get; set; }
[Display(Name = "Minute")]
[FullDateRequired(("Hour"), ("Date"))]
public string Minute { get; set; }
}
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class FullDateRequired : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessage = "{0} is required.";
public string OtherProperty1 { get; private set; }
public string OtherProperty2 { get; private set; }
public FullDateRequired(string otherProperty1, string otherProperty2)
: base(DefaultErrorMessage)
{
OtherProperty1 = otherProperty1;
OtherProperty2 = otherProperty2;
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var otherProperty1 = validationContext.ObjectInstance.GetType().GetProperty(OtherProperty1);
var otherProperty2 = validationContext.ObjectInstance.GetType().GetProperty(OtherProperty2);
var otherProperty1Value = string.IsNullOrEmpty((string)otherProperty1.GetValue(validationContext.ObjectInstance, null));
var otherProperty2Value = string.IsNullOrEmpty((string)otherProperty2.GetValue(validationContext.ObjectInstance, null));
if (string.IsNullOrEmpty((string)value) && (!otherProperty1Value || !otherProperty2Value))
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var clientValidationRule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
ValidationType = "fulldaterequired"
};
clientValidationRule.ValidationParameters.Add("otherproperty1", OtherProperty1);
clientValidationRule.ValidationParameters.Add("otherproperty2", OtherProperty2);
return new[] { clientValidationRule };
}
}
}