好的,所以我正在为 Drupal 7 站点实现 jQuery 验证插件。我遇到的问题是表单的一部分生成了多个名称略有不同的复选框。因此,如果至少选择了这些复选框中的一个,似乎没有直接的方法来检查,使用验证器插件。
http://docs.jquery.com/Plugins/Validation#API_Documentation
更具体地说,用户必须选择一个或多个主题类别。这些复选框的名称如下:“field_themes[und][52]”、“field_themes[und][55]”、“field_themes[und][64]”和“field_themes[und][65]”。
除此之外,我们还有一个与其他复选框无关的复选框,也必须选中。那是为了同意政策等。该插件很容易覆盖该插件,但我认为它会使其他 chekcboxes 的解决方案变得更加棘手。我还有另一组复选框,但它们都使用相同的名称(Drupal 有时很痛苦)。
所以我考虑了一下,认为也许我可以使用submitHandler。所以我想出了下面...
$('#photo-node-form').validate({
rules: {
'field_first_name[und][0][value]': 'required',
'field_last_name[und][0][value]': 'required',
'field_email_address[und][0][email]': {
required: true,
email: true,
},
'field_product_type[und]': 'required',
'field_terms_and_conditions[und]': 'required',
},
messages: {
'field_first_name[und][0][value]': 'Please enter your first name.',
'field_last_name[und][0][value]': 'Please enter your last name.',
'field_email_address[und][0][email]': 'Please enter a valid email address.',
'field_product_type[und]': 'Please select a product.',
'field_terms_and_conditions[und]': 'You must confirm that you are 18 or older and agree to the terms and conditions.',
},
submitHandler: function(form) {
//Verify that at least one theme category is selected.
var themeCatSelected = false;
//First we have to find all of theme
$('#photo-node-form input:checkbox').each(function(i) {
//Make sure this is a theme checkbox
var name = $(this).name;
var isTheme = false;
stristr(name, 'field_themes[und]', isTheme);
if (isTheme && this.checked) {
//See if this is checked
themeCatSelected = true;
}
});
if (!themeCatSelected) {
//Do something to alert the user that is not a popup alert
return false; //Prevent form submittion
}
form.submit();
}
});
//A JS reproduction of the PHP stristr function
function stristr (haystack, needle, bool) {
var pos = 0;
haystack += '';
pos = haystack.toLowerCase().indexOf((needle + '').toLowerCase());
if (pos == -1) {
return false;
}
else {
if (bool) {
return haystack.substr(0, pos);
}
else {
return haystack.slice(pos);
}
}
}
所以我遇到的问题是我不确定如何在正常区域中放置错误以显示此 submitHandler 函数的错误。我什至不确定这是否是最好的方法。有没有人有任何想法?
谢谢!