如何使用Trinidad(JSF)突出显示具有验证错误的输入字段?

时间:2010-07-12 13:35:17

标签: javascript validation jsf trinidad

表单Trinidad的客户端验证失败后,会显示错误消息并突出显示失败输入的标签。我需要突出显示输入字段本身。有可能以某种方式做到这一点吗?我能想到的最绝望的解决方案是在标签上的DOMAttrModified事件上附加js事件监听器,但这真的是一个非常糟糕的黑客。

1 个答案:

答案 0 :(得分:0)

我们最终选择了另一个黑客。加载文档后,我们替换trinidad的函数来提交表单,我们的实现在验证失败后触发事件,然后这个钩子遍历所有验证消息,查找带错误的输入id(并设置它的样式)。丑,但有效。这是使用jQuery(只是草图,未经测试)的想法的说明:

$(window).load(function () {
//save original function for form submit
var originalSubmit = window.submitForm;
//replace function for form submit with custom implementation that triggers some event in case of validation failure
window.submitForm = function (form, doValidate, parameters, isPartial) {
    var retval = originalSubmit(form, doValidate, parameters, isPartial);
    if (!retval) {
        $(window).trigger('failedFormValidations', [form, doValidate, parameters, isPartial]);

    }

}

//bind a listener for failed validations which performs the desired behavior
$(window).bind('failedFormValidations', function(event, form, doValidate, parameters, isPartial) {
    // reset all inputs to nonhighlighted state, then loop through all labels and hightlight inputs with error
});
}