Drupal 8表单改变了ajax回调不起作用

时间:2017-02-07 05:53:36

标签: ajax drupal drupal-modules

我正在以正常的自定义形式获取我的ajax回调,但在表单上更改它不起作用。

function sample_ajax_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'node_sampleajax_form' || $form_id === 'node_sampleajax_edit_form') {
   $form['field_nametrain']= array(
   '#title' => t('training name'),
   '#type' => 'select',
   '#options' => _load_training(),
   '#required' => FALSE,
   '#ajax' => [
    'callback' => [$this, 'changeOptionsAjax'],
    // 'callback' => '::changeOptionsAjax',
    'wrapper' => 'second_field_wrapper',
  ],
 );

 $form['field_namedomain'] = [
  '#type' => 'select',
  '#title' => t('Domain program'),
  '#options' => $this->getOptions($form_state),
    '#prefix' => '<div id="second_field_wrapper">',
    '#suffix' => '</div>',
   ];

 return $form; 
 }
}

 function _load_training() {
 $training = array('- Select domain training -');
 $query = db_select("node__field_trainingname", "a");
 $query->fields("a", array('field_trainingname_value', 'entity_id'));
 $query->orderBy("a.field_trainingname_value");
 $result = $query->execute();

 while($row = $result->fetchObject()){
  $training[$row->entity_id] = $row->field_trainingname_value;
  }
   return $training;
  }

  function changeOptionsAjax(array &$form, FormStateInterface $form_state)   {
   return $form['field_namedomain'];
   } 

   function getOptions(array &$form, FormStateInterface $form_state) {
           $cvvv = $form_state->getValue('field_nametrain');
        <!-- return ["shgsh", $form_state->get(['field_nametrain'])]; -->
  $options = array('- Select subdomain category -');
   $query  = db_select("node__field_trainingname", "a");
   $query->fields("a", array('field_trainingname_value', 'entity_id'));
   $query = db_select("node__field_cms", "b");
   $query->fields("b", array('field_cms_value', 'entity_id'));
   $query->join("node__field_trainingname", "b", "b.entity_id=a.entity_id");
   $query->condition("a.entity_id", $cvvv);
   $result = $query->execute();

  while($row = $result->fetchObject()){
    $options[$row->entity_id] = $row->field_cms_value;
  }

   return $options;
   }

在使用$ this-&gt; getOptions($ form_state)时,它表示错误日志,它不是对象,并在前端引发网站遇到错误。但是在自定义表单上没有错误只出现在形式中它会引发错误。

请建议我在Drupal 8的form_alter中应用的想法

2 个答案:

答案 0 :(得分:0)

表单alter hook所在的.module文件不是类,因此没有$ this。然而,您的自定义表单是一个类(通常在your_module / src / Form / YourForm.php中),这就是它在那里工作但不在.module文件中的原因。

进一步阅读:http://www.php.net/manual/en/language.oop5.basic.phpWhat does the variable $this mean in PHP?

在您的情况下,您应该可以致电

<form>
<textarea rows="10" cols="10" maxlength="200" style="resize:none;"></textarea>
<input type="submit" value="Send message">
</form>

$(document).ready(function() {
    $('textarea').live("keypress", function(e) {
        if (e.keyCode == 13) 
        {
            $('form').submit();
        }
    });
});

还有更多注意事项:我会强烈建议进行一些代码重构。

答案 1 :(得分:0)

在您的自定义提交处理程序中,首先从表单状态获取表单对象。

$formObj = $formState->getFormObject();

然后在表单对象上调用submitForm()并传递表单和表单状态变量。

$formObj->submitForm($form, $formState);

最后,您只需要简单地触发对象上的save()函数即可。

$formObj->save($form, $formState);

因此整个解决方案类似于

function YOR_CUSTOM_SUBMIT_HANLDLER(array $form, FormStateInterface $form_state) {
  /** @var Drupal\user\RegisterForm $entity */
      $formObj = $form_state->getFormObject();
      $formObj->submitForm($form, $form_state);
      $formObj->save($form, $form_state);
}