2013-08-22 93 views
0

我在這個網站上使用多種語言,並希望以不同的語言顯示錯誤。我想知道是否可以在自定義錯誤消息中使用變量。jquery.validate()變量錯誤信息

下面是JavaScript代碼的片段:

$('form').validate({ 
$.ajax({ 
    url: 'notification.php', 
    dataType: 'json', 
    success: function(data) { 
     return notification = data; 
    } 
}); 
rules: { 
    qty: { 
     required: true, 
     digits:  true 
    } 
}, 
messages: { 
    qty: { 
     required: notification['qty_error'], 
     digits:  notification['qty_error2'] 
    } 
}, 
invalidHandler: function() { 
    alert(notification['error2']); 
}, 
submitHandler: function(form) { 
    $.ajax({ 
     url: $('form').attr('action'), 
     type: 'post', 
     data: $('form').serialize(), 
     success: function() { 
      $.ajax({ 
       url: 'notification.php', 
       dataType:'json', 
       success: function(data) { 
        $('#notification .container').html('<div class="success" style="display: none;">'+ data.confirm1 + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); 
        $('#notification .success').fadeIn('slow'); 
       } 
      }); 

     } 
    }); 
} 
}); 

如果沒有什麼是做它的另一種方式。

回答

1

.ajax().validate()之內,就像你在這裏所做的那樣...

$('form').validate({ 
    $.ajax({ ... }); // <-- remove this 
    ... 
}); 

只有指定的規則,選項和回調函數爲每the jQuery Validate plugin documentation可以永遠繼續下去的.validate()內。

我看到你已經在submitHandler回調函數中包含了相同的.ajax()代碼。這是完全正確的,所以只需按照上面的指示移除.ajax()即可。

$('form').validate({ 
    // rules, options & callback functions 
    ... 
    submitHandler: function(form) { 
     $.ajax({ ... }); // <-- ajax always belongs here 
     return false; 
    } 
}); 

至於你原來的問題,,可以替代消息使用變量。

$(document).ready(function() { 

    var message_1 = "my custom message 1"; 

    $('#myform').validate({ 
     rules: { 
      field1: { 
       required: true 
      } 
     }, 
     messages: { 
      field1: { 
       required: message_1 
      } 
     } 
    }); 

}); 

DEMOhttp://jsfiddle.net/PWdke/

動態地改變消息(該插件被初始化之後)將需要the .rules('add') method

$(document).ready(function() { 

    var message_1 = "my custom message 1", 
     message_3 = "my custom message 3"; 

    $('#myform').validate({ 
     rules: { 
      field1: { 
       required: true 
      } 
     }, 
     messages: { 
      field1: { 
       required: message_1 
      } 
     } 
    }); 

    // programatically over-ride initialized rules/messages below 

    $('input[name="field1"]').rules('add', { 
     required: true, 
     messages: { 
      required: message_3 
     } 
    }); 

}); 

DEMOhttp://jsfiddle.net/PWdke/1/

1

您當前的腳本語法不正確,你可以做這樣的事情

var validator = $('form').validate({ 
    rules: { 
     qty: { 
      required: true, 
      digits:  true 
     } 
    }, 
    messages: { 
    }, 
    invalidHandler: function() { 
     alert(notification['error2']); 
    }, 
    submitHandler: function(form) { 
     $.ajax({ 
      url: $('form').attr('action'), 
      type: 'post', 
      data: $('form').serialize(), 
      success: function() { 
       $.ajax({ 
        url: 'notification.php', 
        dataType:'json', 
        success: function(data) { 
         $('#notification .container').html('<div class="success" style="display: none;">'+ data.confirm1 + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>'); 
         $('#notification .success').fadeIn('slow'); 
        } 
       }); 

      } 
     }); 
    } 
}); 
$.ajax({ 
    url: 'notification.php', 
    dataType: 'json', 
    success: function(data) { 
     var notification = { 
      qty: { 
       required: data['qty_error'], 
        digits: notification['qty_error2'] 
      } 
     }; 

     validator.settings.messages = notification 
    } 
}); 
+0

@koala_dev,請不要編輯別人的答案代碼。 – Sparky

+0

Arun,自己放入'.ajax()'...這不會在頁面加載時立即導致ajax提交嗎?他在'submitHandler'回調函數中已經有類似'.ajax()'。 – Sparky

+0

感謝所有的幫助,但我仍然有一個問題。由於變量最初是在PHP中存儲的,並根據所選語言進行了更改,因此我如何將php變量轉換爲JavaScript變量?這就是爲什麼我在開始時使用Ajax,因爲我無法想到另一種方式。 – user2708296

0

語法錯誤:

$('form').validate({ 
$.ajax({ 
    url: 'notification.php', 
    dataType: 'json', 
    success: function(data) { 
     return notification = data; 
    } 
}); 

分開你的Ajax從驗證...

$.ajax({ 
    url: 'notification.php', 
    dataType: 'json', 
    success: function(data) { 
     return notification = data; 
    } 
}); 

$('form').validate({ 
    /*rules: ... */ 
}) 
+0

他不需要「分開」。 '.ajax()'屬於插件的'submitHandler'回調函數。單獨放置會導致頁面加載時發生ajax提交。 – Sparky