0

I am currently constructing a form using an Ajax handler. The problem is that since i used Ajax, the submitting part is run without returning any error message. It is only when I clear the cache that all the errors appear (and the form disappear).

function My_Modile_form($form, $form_state, $arg){
     //constructing the form with REQUIREDfields
  $form ['submit'] = array (
      '#type' => 'submit',
      '#ajax' => array (
          'callback' => 'mymodule_form_ajax_callback',
          'wrapper' => 'result-div' 
      ),
      '#value' => t ( 'Lancer la simulation' ),
      '#weight' => 10000 
  );
}

function My_Module_form_validate($form_id, $form) {

  $form_numbers = array (
      'field_one',
      'field_two' 
  );
  foreach ( $form_numbers as $form_number ) {
    $numb = $form ['input'] [$form_number];
    if (! is_numeric ( $numb )) {
      form_set_error ( $form ['complete form'] [$form_number] ['#title'], 'ERROR !' );
    }
  }
}

function mymodule_form_ajax_callback($form, $form_state) {
  $form ['simul_message_retour'] += mymodule_form_submit ( $form, $form_state );
  return $form ['simul_message_retour'];
}

function mymodule_form_submit($form, $form_state) {
    // Submitting part
}

Has anyone met this issue before with Ajax forms ?

Thank you,

EDIT: I just found that the hook_form_submit is called BEFORE the hook_form_validate

1

Here you have the framework for Form-handling:

function customregister_custom_form($form, &$form_state){
  $form['first_name'] = array(
    '#title' => t('Vorname'),
    '#type' => 'textfield',
    '#default_value' => '0',
    '#required' => TRUE
  );
  $form['last_name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => '',
    '#required' => TRUE
  );
  $form ['submit'] = array (
    '#type' => 'submit',
    '#ajax' => array (
    'callback' => 'customregister_custom_form_callback',
      'wrapper' => 'customregister-custom-form'
    ),
    '#value' => t ( 'Lancer la simulation' ),
    '#weight' => 10000
  );
  return $form;
}

function customregister_custom_form_validate($form, &$form_state) {

  if (! is_numeric ( $form_state['values']['first_name'] )) {
    form_set_error ( 'first_name', 'ERROR !' );
  }
  if (! is_numeric ( $form_state['values']['last_name'] )) {
    form_set_error ( 'last_name', 'ERROR !' );
  }
}

function customregister_custom_form_callback($form, &$form_state) {
  drupal_set_message('custom_form_callback');
  //customregister_custom_form_submit($form, $form_state);
  $formular = drupal_get_form('customregister_custom_form');
  $formular = render($formular);
  return $formular;
}

function customregister_custom_form_submit($form, &$form_state) {
  drupal_set_message('custom_form_submit');
}
  • Unfortunatly I did. The function is called but the error message is not shown when Javascript is activated in the browser. I change the post accordingly – Ismail H Nov 2 '15 at 10:18
  • I dont't see any form-values in your implementation. Usually all form-values are stored per reference in $form_state['values'] – Paul Bönisch Nov 2 '15 at 10:21
  • Oh yes i see now. I tried it too. It is still the same. I think it has something to do with the fact that i've put the form inside a block. It might be caching the block. need to investigate more this way – Ismail H Nov 2 '15 at 10:29
  • check my last edit about setting the error – Paul Bönisch Nov 2 '15 at 10:31
  • Did change the parameters in the form_set_error(). Not solved. – Ismail H Nov 2 '15 at 10:35
1

Try this:

function my_module_form($form, &$form_state) {
  $form['#prefix'] = '<div id="my_module_form_wrapper" class="wrapper-for-form">';
  $form['#suffix'] = '</div>';
  $form['other_elements'] = array(
      //code...
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#weight' => 5,
    '#value' => t('Submit'),
    '#validate' => array('my_module_form_validate'),
    '#ajax' => array(
      'callback' => 'my_module_form_submit',
      'wrapper' => 'my_module_form_wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );
}

function my_module_form_validate($form, &$form_state) {
  //Your validation logic
  $form_numbers = array(
    'field_one',
    'field_two'
  );
  foreach ($form_numbers as $form_number) {
    $numb = $form ['input'] [$form_number];
    if (!is_numeric($numb)) {
      form_set_error($form ['complete form'] [$form_number] ['#title'], 'ERROR !');
    }
  }
}

function my_module_form_submit($form, &$form_state) {
  drupal_validate_form('my_module_form', $form, $form_state);
  if (form_get_errors()) {
    $form_state['rebuild'] = TRUE;
    $form['your_elements']['#value'] = $form_state['input']['elements'];
    return $form;
  }
  else {
    // continue with your submit logic.
  }
}
  • I added the drupal_validate_form() function you proposed. As i manage the form with a bloc, the return $form; returned the form (again) showing two times the form. It would have been perfect to have something like refresh($form). Thanks for your answer ! – Ismail H Nov 2 '15 at 16:00
  • Check your elements on page. and check whether the wrapper is in the place. that is where the form is get replaced after process. This line in the code: 'wrapper' => 'my_module_form_wrapper', – RDP Nov 3 '15 at 4:38
  • I've added updated code. Check prefix and suffix to form. This will replace your form on response. – RDP Nov 3 '15 at 4:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.