0

I have a simple form with ajax submit:

function activit_registration_form($form, $state) {

  $form["ajax-start"] = array("#markup" => "<div id='registration-form-ajax'>");

  // ... some form elements are here            

  $form["submit"] = array(
      "#type" => "submit",
      "#value" => t("Odoslať"),
      "#ajax" => array(
          "callback" => "activit_registration_form_ajax",
          "wrapper" => "registration-form-ajax",
          "effect" => "fade"));

  $form["ajax-end"] = array("#markup" => "</div>");

  return $form;

}

Ajax callback:

function activit_registration_form_ajax($form, $state) {

  return $form;
}

Validation:

function activit_registration_form_validate($form, $state) {

  if(!valid_email_address($state['input']['email'])) { form_set_error('email', 'email'); }
  if(strlen(strval($state['input']['name']))<3) { form_set_error('name', 'meno'); }
  if(strlen(strval($state['input']['tel']))<6) { form_set_error('tel', 'tel'); }

}

Ajax callback is now returning the same form, but I would like to achieve returning just a simple string instead of the form when form does not contain any errors.

What is the best way to recognize if a form contains errors or not in ajax callback?

  • What's the purpose of the simple string? Should it replace the form as a message or you want to handle the string with JavaScript? – Jimmy Ko Jul 13 '16 at 11:02
  • Your edit is not that fit. I have modified it and please take a try. – Jimmy Ko Jul 13 '16 at 11:40
0

You can define the form status in $form_state and check the status during rebuilding form.

Example code:

function activit_registration_form($form, $state) {

  if (isset($state['submit_success'])) {
    $form['ajax_form']['message'] = array("#markup" => t('Thanks for your submission.'));
  }
  else {

    $form["ajax_form"] = array(
      "#prefix" => "<div id='registration-form-ajax'>",
      "#suffix" => "</div>",
    );

    // define form elements under 'ajax_form', such as
    // $form["ajax_form"]["element_1"] = array( ... );

    $form['ajax_form']["submit"] = array(
      "#type" => "submit",
      "#value" => t("Odoslať"),
      "#ajax" => array(
        "callback" => "activit_registration_form_ajax",
        "wrapper" => "registration-form-ajax",
        "effect" => "fade",
      ),
    );
  }

  return $form;
}

function activit_registration_form_ajax($form, $state) {
     return $form['ajax_form'];
}

function activit_registration_form_validate($form, &$state) {
  $has_error = FALSE;

  if (!valid_email_address($state['input']['email'])) {
    form_set_error('email', 'email');
    $has_error = TRUE;
  }
  if (strlen(strval($state['input']['name']))<3) {
    form_set_error('name', 'meno');
    $has_error = TRUE;
  }
  if (strlen(strval($state['input']['tel']))<6) {
    form_set_error('tel', 'tel');
    $has_error = TRUE;
  }

  $state['submit_success'] = !$has_error;

  $state['rebuild'] = TRUE;
}

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.