When using ajax login form we are getting the warning "Error: Invalid form POST data" to resolve that we need to implement hook_exit()
/**
* Implements hook_exit().
*/
function module_exit($destination = NULL) {
if (arg(0) == 'system' && arg(1) == 'ajax') {
$is_user_login_form_submission = isset($_POST) && isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['form_build_id']);
if ($is_user_login_form_submission && user_is_anonymous()) {
$form_build_id = $_POST['form_build_id'];
$form_state = form_state_defaults();
$form_state['values'] = $_POST; // Important!
$form = form_get_cache($form_build_id, $form_state);
if (!$form) {
watchdog(__FUNCTION__, 'User login AJAX form submission failed. Trying again...', array(), WATCHDOG_WARNING);
$form = drupal_rebuild_form('user_login_form', $form_state);
$form['#build_id_old'] = $form['#build_id']; // Important!
// Try form submission again after it is rebuilt above
$commands[] = ajax_command_update_build_id($form);
$commands[] = ajax_command_invoke('form#user-login-form', 'trigger', array('submit'));
print ajax_render($commands);
}
}
}
}
Refference Link
https://www.drupal.org/node/1939254
http://drupal.stackexchange.com/questions/152785/invalid-form-post-data-ajax-for-authenticated-users
http://drupal.stackexchange.com/questions/36830/invalid-form-post-data-in-ajax-login-form
/**
* Implements hook_exit().
*/
function module_exit($destination = NULL) {
if (arg(0) == 'system' && arg(1) == 'ajax') {
$is_user_login_form_submission = isset($_POST) && isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['form_build_id']);
if ($is_user_login_form_submission && user_is_anonymous()) {
$form_build_id = $_POST['form_build_id'];
$form_state = form_state_defaults();
$form_state['values'] = $_POST; // Important!
$form = form_get_cache($form_build_id, $form_state);
if (!$form) {
watchdog(__FUNCTION__, 'User login AJAX form submission failed. Trying again...', array(), WATCHDOG_WARNING);
$form = drupal_rebuild_form('user_login_form', $form_state);
$form['#build_id_old'] = $form['#build_id']; // Important!
// Try form submission again after it is rebuilt above
$commands[] = ajax_command_update_build_id($form);
$commands[] = ajax_command_invoke('form#user-login-form', 'trigger', array('submit'));
print ajax_render($commands);
}
}
}
}
Refference Link
https://www.drupal.org/node/1939254
http://drupal.stackexchange.com/questions/152785/invalid-form-post-data-ajax-for-authenticated-users
http://drupal.stackexchange.com/questions/36830/invalid-form-post-data-in-ajax-login-form
No comments:
Post a Comment