Drupal 8自定义表单使用Javascript提交覆盖

时间:2019-07-08 15:43:50

标签: javascript drupal-8 drupal-modules drupal-form-submission

我已经在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);

0 个答案:

没有答案