不显眼的验证,自定义数据注释不起作用



我被困在.net mvc 5项目中的一些自定义数据验证中。我创建了自定义 ValidateAgeAttribute.cs其中包含一些自定义数据验证。而且,我有一个自定义验证.js文件,我在其中添加了方法和适配器。无法使其工作...不开火。其他验证(开箱即用(在同一表单上以不显眼的方式正常工作。它仅在自定义上失败。非常欢迎任何帮助或建议。

提前谢谢。

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class ValidateAgeAttribute : ValidationAttribute, IClientValidatable
{
private const string DefaultErrorMessage = "Your age is invalid, your {0} should fall between {1} and {2}";
public DateTime MinimumDateProperty { get; private set; }
public DateTime MaximumDateProperty { get; private set; }
public ValidateAgeAttribute(
int minimumAgeProperty,
int maximumAgeProperty)
: base(DefaultErrorMessage)
{
MaximumDateProperty = DateTime.Now.AddYears(minimumAgeProperty * -1);
MinimumDateProperty = DateTime.Now.AddYears(maximumAgeProperty * -1);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
DateTime datevalue;
if (value != null)
{
if (DateTime.TryParse(value.ToString(), out datevalue))
{
if (value != null)
{
DateTime parsedValue = (DateTime)datevalue;
if (parsedValue <= MinimumDateProperty || parsedValue >= MaximumDateProperty)
{
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
}
}
}
return ValidationResult.Success;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule()
{
ValidationType = "validateage",
ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
};
rule.ValidationParameters.Add("minumumdate", MinimumDateProperty.ToShortDateString());
rule.ValidationParameters.Add("maximumdate", MaximumDateProperty.ToShortDateString());
return new[] { rule };
}
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name, MinimumDateProperty.ToShortDateString(), MaximumDateProperty.ToShortDateString());
}
}

JS:

$(document).ready(function () {
$.validator.addMethod(
'validateage',
function (value, element, params) {
return this.optional(element) || (dateConverter(value) >= dateConverter(params.minumumdate) && dateConverter(value) <= dateConverter(params.maximumdate));
});
$.validator.unobtrusive.adapters.add(
'validateage', ['minumumdate', 'maximumdate'], function (options) {
var params = {
minumumdate: options.params.minumumdate,
maximumdate: options.params.maximumdate
};
options.rules['validateage'] = params;
options.messages['validateage'] = options.message;
});
});
function dateConverter(value) {
var valueParts = value.split("/");
return new Date(+valueParts[2], valueParts[1] - 1, +valueParts[0]);
}

脚本调用顺序:

<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/shared/custom-validators.js"></script>

和渲染的视图输入:

<input class="form-control text-box single-line valid" data-val="true" data-val-validateage="Date de naissance incorrecte" data-val-validateage-maximumdate="07/01/2020" data-val-validateage-minumumdate="07/01/1925" id="Birthdate" name="Birthdate" type="text" value="" aria-describedby="Birthdate-error" aria-invalid="false">

我发现了这个问题。我在与视图相关的 js 中称我的帮助程序操作为"revindvalidators",但它很快就触发了,我将其放在验证器添加之后。现在它起作用了。

$(document).ready(function () {
// The validateage function
$.validator.addMethod(
'validateage',
function (value, element, params) {
return this.optional(element) || (dateConverter(value) >= dateConverter(params.minumumdate) && dateConverter(value) <= dateConverter(params.maximumdate));
});
$.validator.unobtrusive.adapters.add(
'validateage', ['minumumdate', 'maximumdate'], function (options) {
var params = {
minumumdate: options.params.minumumdate,
maximumdate: options.params.maximumdate
};
options.rules['validateage'] = params;
options.messages['validateage'] = options.message;
});
helper.rebinvalidators();
});
function dateConverter(value) {
var valueParts = value.split("/");
return new Date(+valueParts[2], valueParts[1] - 1, +valueParts[0]);
}

我的帮助程序文件中的重新绑定验证器方法包含以下代码:

rebinvalidators: function() {
var $form = $("#formId");
$form.unbind();
$form.data("validator", null);
$.validator.unobtrusive.parse($form);
$form.validate($form.data("unobtrusiveValidation").options);
},

相关内容

  • 没有找到相关文章

最新更新