在表单验证drupal之后执行AJAX回调



我在drupal 8中具有形式,在hook_form_alter中,我正在尝试提出ajax请求。

function helloworld_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'contact_message_feedback_form') {
$form['actions']['submit']['#ajax'] = array(
  'wrapper' => 'form_wrapper',
  'callback' => 'custom_callback',
  'event' => 'click',
  'progress' => [
  'type' => 'throbber',
  'message' => 'Veryfieng'],
  );  
$form['test_selection'] = [
  '#type' => 'hidden', 
  '#prefix' => '<div id="form_wrapper">',
  '#suffix' => '</div>',
  '#weight' => 100,
];
  }
} 

 function custom_callback($form, &$form_state) {
   $output = array();
   $output['#markup'] = 'Thank you! Your message has been sent!';
   return $output;
 }

这有效,但整个验证功能被忽略。在调用custom_callback((之前,如何验证表单?

而不是使用自定义AJAX回调,请尝试以下代码,它将调用表单的submitform((方法,因此在调用submitform(( -

$form['actions']['submit'] = array(
  '#attributes' => array (
     'class' => array (
         'use-ajax-submit'
     ), 
  ),
  '#ajax' => array(
     'wrapper' => 'form_wrapper',
  )
);  

简而言之,它将通过Ajax提交您的表格。

最新更新