表单提交Drupal后在模态对话框中显示响应8

时间:2019-12-24 16:40:21

标签: php drupal-8

我想在drupal 8中提交表单api后在弹出窗口中显示成功消息,我尝试了许多解决方案,但我仍然不知道,

我以编程方式设置了表单

如果有人可以帮助男人,将不胜感激。

  

这就是我所做的

public function buildForm(array $form, FormStateInterface $form_state)
  {

    $form['nom'] = array(
      '#type' => 'textfield',
      '#title' => t('le nom de candidat:'),
      '#required' => TRUE,
    );
    $form['prenom'] = array(
      '#type' => 'textfield',
      '#title' => t('le prénom de candidat:'),
      '#required' => TRUE,
    );
    $form['telephone'] = array(
      '#type' => 'tel',
      '#title' => t('numéro de telephone'),
    );
  $form['actions']['cancel'] = array(
      '#type' => 'button',
      '#value' => t('Retourner à la page précédente'),
      '#prefix' => '     ',
      '#attributes' => array('onClick' => 'history.go(-1); return true;','style' => 'width: 36%;'),
      //'#post_render' => array('change_button_type'),
    );




    $form['actions']['cancel']['#attributes']['class'][] = 'btn-danger';

    return $form;
  }

1 个答案:

答案 0 :(得分:-2)

use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;



public function buildForm(array $form, FormStateInterface $form_state) {

/*....
.....
..this is my form submit button where i'm calling an ajax callback...*/
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#prefix' => '<div id="grievance_form_wrapper">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => '::_modal_form_grievance_ajax_submit',
'event' => 'click'
),  
'#value' => $this->t('Submit'),
); 
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';// Attach the library for 
pop-up dialogs/modals.
return $form;
}

function _modal_form_grievance_ajax_submit(array $form, FormStateInterface 
&$form_state) {
$response = new AjaxResponse();
if ($form_state->getErrors()) {
unset($form['#prefix']);
unset($form['#suffix']);
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,];
$response->addCommand(new HtmlCommand('#grievance_form_wrapper', $form));
}
else {
$content = 'Something to show in the modal';
$title = 'MY MODAL FORM';
$response = new AjaxResponse();
$response->addCommand(new OpenModalDialogCommand($title, $content, 
array('width'=>'700')));
}
return $response;
}