Friday 2 October 2015

How to validate and submit a form using AJAX in Drupal ?

Drupal convert your form in to ajax 
 
Below is the example of ajaxifying your drupal form. all you have to do is write the call back
wrapper Form api will ajaxify your form.  
 
function test_form($form, &$fstate) {
  $form["wrapper"] = array("#markup" => "<div id='test-ajax'></div>");
  $form["name"] = array("#type" => "textfield", "#required" => true, "#title" => "Name");
  $form["submit"] = array(
    "#type" => "submit", 
    "#value" => "Send", 
    "#ajax" => array(
    "callback" => "test_form_callback", 
    "wrapper" => "test-ajax",
    "effect" => "fade"));
  return $form;
}

function test_form_callback($form, &$fstate) {
  return "<div id='test-ajax'>Wrapper Div</div>";
}

function form_validate($form, &$fstate) {
  form_set_error("name", "your  error to display.");
}
 
Reference Links: 
 
http://drupal.stackexchange.com/questions/12289/how-to-validate-and-submit-a-form-using-ajax

http://drupal.stackexchange.com/questions/6327/how-to-ajaxify-webform-submit-in-drupal-7
 
https://www.drupal.org/node/2081275

https://gist.github.com/pascalduez/1517513

No comments:

Post a Comment