わかりました、Drupal 7 サイトの jQuery 検証プラグインを実装しています。私が直面している問題は、フォームの一部が、わずかに異なる名前の複数のチェックボックスを生成することです。このため、バリデータ プラグインを使用して、これらのチェックボックスの少なくとも 1 つが選択されているかどうかを直接確認する方法はないようです。
http://docs.jquery.com/Plugins/Validation#API_Documentation
より具体的には、ユーザーは 1 つ以上のテーマ カテゴリを選択する必要があります。これらのチェックボックスには、「field_themes[und][52]」、「field_themes[und][55]」、「field_themes[und][64]」、および「field_themes[und][65]」のような名前が付いています。
これに加えて、チェックする必要がある他のチェックボックスとは関係のないチェックボックスもあります。それは、ポリシーなどに同意するためのものです。これはプラグインで簡単にカバーできますが、他のチェックボックスのソリューションは少しトリッキーになると思います。別のチェックボックスのセットもありますが、それらはすべて同じ名前を使用しています (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 関数のエラーを表示するために、通常の領域にエラーを配置する方法がわからないことです。これがこれを行うための最良の方法であるかどうかさえわかりません。誰にもアイデアはありますか?
ありがとう!