0

私は jQuery Validation Engine を使用しています (開発者には特に感謝しています)。問題は次のとおりです。フィールドのグループを必須にする必要がありますが、そのうちの少なくとも 1 つが入力されている場合にのみ必要です。

_validateField関数内に次のコードを追加しました。

case "ifOneAllGroupRequired":  
// Check is its the first of group, if not, reload validation with first field
// AND continue normal validation on present field
var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";

errorMsg = methods._ifOneAllGroupRequired(field, rules, i, options);
if(errorMsg) { required = true; }
options.showArrow = false;
break;

そして次の機能を作りました:

_ifOneAllGroupRequired: function(field, rules, i, options) {
var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";
var isValid = 0 , getTotal = 0;

// Check all fields from the group
field.closest("form").find(classGroup).each(function(){
    getTotal++;     // count all the fields
    if(! methods._required($(this), rules, i, options) ){
        isValid++;  // count the fields filled
    }
});

// if at least 1 is filled and the other arent launch error
if( isValid != getTotal && isValid != 0 ) return options.allrules[rules[i]].alertText;
// if they are, clean all errors
else field.closest("form").find(classGroup).validationEngine('hide');
},

私はこれを機能させました。問題は、「groupRequired」オプションのように最初のフィールドではなく、すべてのフィールドの上にエラーが発生することです(validationEngineのデフォルト)

私には手がかりがなく、方向転換する必要があります。何か案は?

4

0 に答える 0