IE中的jQuery.validate错误

时间:2011-05-19 08:35:03

标签: asp.net-mvc-2 internet-explorer-8 internet-explorer-7 jquery-validate

我有一个MVC应用程序,我添加了自定义跨字段验证。跨字段验证未配置为客户端,但是当我通过我的字段选项卡时,IE会从jquery.validate中抛出以下错误“$ .validator.method [...]为空或不是对象” .js文件。我已附上完整版本,因此我可以调试正在进行的操作,它似乎试图在客户端下面触发我的“mandatoryif”自定义验证,然后将错误抛出到以下行:

var result = $.validator.methods[method].call( this, element.value.replace(/\r/g, ""), element, rule.parameters );  

为什么在没有在客户端添加“mandatoryif”验证时尝试这样做的任何想法?

我已经更新到最新版本,因为我读到它可能是jQuery.validate的版本,但这也没有解决问题。

这是我的自定义验证: 属性

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class MandatoryIfAttribute : ValidationAttribute, ICrossFieldValidationAttribute
{       
    public string OtherProperty { get; set; }

    public bool IsValid(ControllerContext controllerContext, object model, ModelMetadata modelMetadata)
    {
        if (model == null)
        {
            throw new ArgumentNullException("model");
        }

        // Find the value of the other property.
        var propertyInfo = model.GetType().GetProperty(OtherProperty);
        if (propertyInfo == null)
        {
            throw new InvalidOperationException(string.Format("Couldn't find {0} property on {1}.",
                                                              OtherProperty, model));
        }

        var otherValue = propertyInfo.GetGetMethod().Invoke(model, null);

        if (modelMetadata.Model == null)
        {
            modelMetadata.Model = string.Empty;
        }

        if (otherValue == null)
        {
            otherValue = string.Empty;
        }

        return (String.IsNullOrEmpty(modelMetadata.Model.ToString()) && (String.IsNullOrEmpty(otherValue.ToString()) || otherValue.ToString() == "0")) || (!String.IsNullOrEmpty(modelMetadata.Model.ToString()) && String.IsNullOrEmpty(otherValue.ToString())) || (!String.IsNullOrEmpty(modelMetadata.Model.ToString()) && !String.IsNullOrEmpty(otherValue.ToString()));
    }

    public override bool IsValid(object value)
    {
        // Work done in other IsValid
        return true;
    }

验证

public class MandatoryIfValidator : CrossFieldValidator<MandatoryIfAttribute>
{       
    public MandatoryIfValidator(ModelMetadata metadata, ControllerContext controllerContext,
                                    MandatoryIfAttribute attribute) :
        base(metadata, controllerContext, attribute)
    {
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ValidationType = "mandatoryif",
            ErrorMessage = Attribute.FormatErrorMessage(Metadata.PropertyName),
        };

        rule.ValidationParameters.Add("otherProperty", Attribute.OtherProperty);

        return new[] { rule };
    }

感谢您的任何帮助。

1 个答案:

答案 0 :(得分:1)

我设法找到了查询的答案。因为我的一些自定义验证是对数据库进行查找,所以我不希望客户端进行此操作的开销,因此没有客户端代码。但是,我没有意识到添加

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(MandatoryIfAttribute), typeof(MandatoryIfValidator));

在Global.asax中

,它正在影响添加此客户端。当然,因为没有代码来处理客户端,所以它会抛出validaiton错误。