1

わかりました、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 関数のエラーを表示するために、通常の領域にエラーを配置する方法がわからないことです。これがこれを行うための最良の方法であるかどうかさえわかりません。誰にもアイデアはありますか?

ありがとう!

4

1 に答える 1

2

次のことを行う必要があります。

  1. チェックボックスのグループを作成する
  2. カスタム検証メソッドを作成して、それらの少なくとも 1 つがチェックされていることを確認します
  3. 各チェックボックスのルール オブジェクトにルールを追加します。
  4. 関数をカスタマイズしerrorPlacementてエラーを適切な場所に配置する

これは #2 のコードです (セレクターの仕組みについては、こちらを参照してください)。

$.validator.addMethod("custom_checks", function(value, element) {
    return $('#photo-node-form input[name^="field_themes[und]"]:checked').length > 0;
}, 'Please check one of these');

#1 と #3 の一部のコードは次のとおりです。

var tmpChecks = [], myRules = {
 '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'
};
$('#photo-node-form input[name^="field_themes[und]"]').each(function(){
   tmpChecks.push(this.name); //This is the start of #1
   myRules[$(this).attr("name")] = {
    custom_checks: true  
   }; //#3
});

#4 では、これを検証オブジェクトに追加します。

errorPlacement: function(error, element) {
    if (element.attr("name").match(/^field_themes\[und\]/)){
   $('#checkbox_errors').html(error);
    } else {
   error.insertAfter(element);
    }
}

次のように検証を呼び出すことになります。

$("#photo-node-form").validate({
    rules: myRules,
    groups: {
        checks: tmpChecks.join(' ')   //the rest of #1
    },
    errorPlacement: function(error, element) {
        if (element.attr("name").match(/^field_themes\[und\]/)){
            //I added this span after all the textboxes, you can figure this out in a more dynamic way if you prefer               
           $('#checkbox_errors').html(error); 
        } else {
           error.insertAfter(element);
        }
    }
});

これは、より単純なフォームでの実際の例です: http://jsfiddle.net/ryleyb/nUbGk/1/

于 2012-12-04T17:10:44.587 に答える