0

I've got a form where I'm using jQuery validation on its elements. The form contains two text fields and a set of checkboxes. The relevant part of my code looks like this:

$.validator.addMethod('require-one', function (value) {
    return $('.require-one:checked').size() > 0;
}, 'Who should see your question?');

var checkboxes = $('.require-one');
var checkbox_names = $.map(checkboxes, function(e,i) { return $(e).attr('name')}).join(' ');

$('FORM#node-form').validate({
    groups: { checks: checkbox_names },
    errorPlacement: function(error, element)
    {
        if (element.attr('type') == 'checkbox') {
            error.insertAfter('#user_role_buttons > div > label');
        }
        else {
            error.insertAfter(element);
        }
    }
});

The new method and "checkboxes" stuff is required for the checkboxes to be handled as a group, and that's actually working correctly, including the placement of the error message inside that #user_role_buttons thing.

My problem/question is that I can't find any way to set the placement of error messages for the two text elements. The validation is working correctly for those items; I just can't specify their position. In particular, errors involving those items aren't showing up in the errorPlacement clause of the validate handler: If I put a console.log(element) statement inside the errorPlacement's function to see what's going on, the only thing that shows up is the error involving the checkboxes (assuming there is one). The messages for the text items are getting placed somewhere semi-reasonable, but not where I want them.

I must be missing something; can anybody see what it is?

4

1 回答 1

0

好的,我不确定这是否“正确”,但我有一些工作。在这一点上,我的信念是,一旦你为“组”定义了一些东西,你就必须将你想要验证的所有东西定义为一个组,即使它只是一个东西。所以上述代码的工作版本如下所示:

$.validator.addMethod('require-one', function (value) 
                                                { return $('.require-one:checked').length > 0; }, 
                                                'Who should see your question?'
                              );
var checkboxes = $('.require-one');
var checkbox_names = $.map(checkboxes, function(e,i) { return $(e).attr('name')}).join(' ');
$('FORM#node-form').validate({
                        groups: { 
                            checks: checkbox_names,
                            title: 'title',
                            body: 'body',
                            },
                        errorPlacement: function(error, element) {
                                 if (element.attr('type') == 'checkbox') {
                                    error.insertAfter('#user_role_buttons > div > label');
                                 }
                                 else if (element.attr('type') == 'text') {
                                    error.insertAfter('#edit-title-wrapper > label');
                                 }
                                 else if (element.attr('name') == 'body') {
                                    error.insertAfter('#edit-body-wrapper > label');
                                 }
                        }         
            });

也就是说,我将“标题”和“正文”定义为组,每个组都指向字段的“名称”。然后这些出现在 errorPlacement 处理程序的调用中,我可以测试它们的存在并按照我想要的方式分配定位。和我预期的不太一样,但我想事后它有点道理......

于 2012-07-18T15:59:12.440 回答