0

我在 Drupal 8 中编写了一个自定义模块,该模块具有与 Paymetric 的 javascript 集成。我正在使用 javascript 处理此表单的提交,但问题是,它会触发表单验证,但从不提交表单。另一方面,当我使用表单的自定义 drupal 提交按钮提交它时,它可以正常运行。我在我的几个其他模块中使用 javascript 处理表单提交,没有任何问题。有人可以帮我找出为什么会发生这种情况吗?

检查是否有任何隐藏字段是必需的并限制我们提交表单,还检查了控制台中的任何 javascript 错误,如果这可能以任何方式影响,我将在表单构建和构造时终止缓存。还确保提取并提交具有正确 ID 的表格。

这是我的代码:

Custom Module File:
/*
 -- namespace defined and Drupal/Symphony classes used --
*/
class PurchaseCartContact extends FormBase {
  protected $account;
  protected $tempStore;
  protected $messenger;

  public function __construct(
    AccountInterface $account,
    PrivateTempStoreFactory $tempStore,
    MessengerInterface $messenger
  ) {

    \Drupal::service('page_cache_kill_switch')->trigger();

    $this->account      = $account;
    $this->tempStore    = $tempStore->get('org_purchases');
    $this->messenger    = $messenger;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('current_user'),
      $container->get('tempstore.private'),
      $container->get('messenger')
    );
  }

  public function getFormId() {
    return 'purchase_cart_contact';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    //$form = array();
    $form['#cache']['max-age'] = 0;

    $tempstore                = \Drupal::service('tempstore.private')->get('org_purchases');

 // custom logic and form fields defined

    $form['submit'] = array(
          '#type' => 'submit',
          '#value' => $this->t('Submit'),
          '#attributes' => [
            'class' => ['btn btn-primary payment-submit'],
          ],
          '#prefix' => '<div class="form-item">',
          '#suffix' => '</div>',
        );
    return $form;

  } // buildForm ends here


  public function validateForm(array &$form, FormStateInterface $form_state) {
    ksm("inside validate");
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    ksm("inside submit");
  }
}

树枝文件:

<div id="action-nav" class="button-group form-item element">
        {# <div class="pull-left"> {{ form.submit }} </div> #} {# works as expected #}
        <div class="btn btn-primary" style="width: 150px;" id="edit-submitpurchasepayment">SUBMIT</div>
    </div>
{{ form.form_token}}
{{ form.form_build_id }}
{{ form.form_id }}

Javascript 文件:

(function ($, Drupal) {
    Drupal.behaviors.org_purchase = {
        attach: function (context, settings) {
            form = document.getElementById('purchase-cart-contact');
            form.submit();
 }
    };
})(jQuery, Drupal);
4

0 回答 0