jquery validate “:hidden”

jquery validate default setting ignore ":hidden", after version 1.9.0. http://bassistance.de/2011/10/07/release-validation-plugin-1-9-0/


在 asp.net mvc使用 [attribute]验证输入数据的时候,有个 radio button一直没有触发验证…卡了半天,把页面其他字段拿掉还是不动,一怒之下把 html, javascript复制到 jsbin居然就能跑?
想不到什么好方法,一项一项比对差异,本机 jquery.validate是 1.10,jsbin上拿别人的范例来改是载入 cdn的 1.8,嗯……体力耗尽,改日再战 Orz

登登登,一日过去。

$("form").validate().element("#my_column") // 用这个方法试我的字段到底有没有验证,得到 js执行错误,好的开始!
// 发现它会去调用内部的 check()
// check()会调用 validationTargetFor()

validationTargetFor: function(element) {
    // if radio/checkbox, validate first element in group instead
    if (this.checkable(element)) {
        element = this.findByName( element.name ).not(this.settings.ignore)[0];
    }
    return element;
},

// this.settings.ignore = ":hidden" 很熟悉的 jquery selector
// 回头看 css,设计为了自订 radio button style的确有隐藏 radio & checkbox

确定原因,解法就单纯了:

// http://stackoverflow.com/a/8565769/4963421
$.validator.setDefaults({
    ignore: []
});
// or
$(function () {
    $('#myform').validate({ // 好像因为 unobtrusive的关系,原本的默认值有被另外复制一份,所以这样改不会生效
        ignore: [],
        // any other options and/or rules
    });
    
    $("form").data("validator").settings.ignore = [];// 要这样硬来
})

终于动了 Q_Q

顺便记录 MVC自订验证逻辑步骤:

// checkbox = true检查 elemNames、checkbox = false检查 falseCheckElemNames
public class CheckBoxConditionRequiredAttribute : ValidationAttribute, IClientValidatable
{
    private const string _defaultErrorMessage = "The Value field is required.";
    private string[] _elemNames;
    private string[] _falseCheckElemNames;

    public CheckBoxConditionRequiredAttribute(string[] elemNames, string[] falseCheckElemNames = null)
    {
        _elemNames = elemNames;
        _falseCheckElemNames = falseCheckElemNames;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null)
        {// checkbox 没值不检查
            return null;
        }

        bool result = true, chkValue = false;
        bool.TryParse(value.ToString(), out chkValue);

        if (chkValue)
        {
            foreach (var item in _elemNames)
            {
                var itemValue = Convert.ToString(validationContext.ObjectType.GetProperty(item).GetValue(validationContext.ObjectInstance, null));
                if (string.IsNullOrEmpty(itemValue))
                {
                    result = false;
                    break;
                }
            }
        }
        else
        {// false时要填的字段
            if (_falseCheckElemNames != null)
            {
                foreach (var item in _falseCheckElemNames)
                {
                    var itemValue = Convert.ToString(validationContext.ObjectType.GetProperty(item).GetValue(validationContext.ObjectInstance, null));
                    if (string.IsNullOrEmpty(itemValue))
                    {
                        result = false;
                        break;
                    }
                }
            }
        }
        if (!result)
        {
            string errorMsg = this.ErrorMessageString;
            if (string.IsNullOrEmpty(this.ErrorMessage))
            {
                if (this.ErrorMessageResourceType != null && !string.IsNullOrEmpty(this.ErrorMessageResourceName))
                {
                    errorMsg = ErrorMessageResourceType.GetProperty(ErrorMessageResourceName).GetValue(ErrorMessageResourceType) as string;
                }
                else
                {
                    errorMsg = _defaultErrorMessage;
                }
            }
            return new ValidationResult(errorMsg);
        }

        return null;
    }

    public IEnumerable GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        string errorMsg = this.ErrorMessageString;
        if ((this.ErrorMessageResourceType == null || string.IsNullOrEmpty(this.ErrorMessageResourceName)) && string.IsNullOrEmpty(this.ErrorMessage))
        {
            errorMsg = _defaultErrorMessage;
        }

        ModelClientValidationRule rule = new ModelClientValidationRule
        {
            ErrorMessage = string.Format(errorMsg),
            ValidationType = "checkboxconditionrequired"
        };

        // 此参数一定要是小写!
        rule.ValidationParameters.Add("obj", metadata.PropertyName);
        rule.ValidationParameters.Add("domelemnames", string.Join(",", _elemNames));
        rule.ValidationParameters.Add("falsecheckelem", string.Join(",", _falseCheckElemNames));
        yield return rule;
    }
}

[CheckBoxConditionRequired(new string[] { "input_while_true" }, falseCheckElemNames: new string[] { "input_while_false" }
    , ErrorMessageResourceType = typeof(resourceType)
    , ErrorMessageResourceName = "resourceProperty"
)]
public bool flag { get; set; }
public string input_while_true { get; set; }
public string input_while_false { get; set; }