I have a page with a simple view. Above the view there is a menu link that when clicked loads a node form with a required file field. The form is fetched and loaded using ajax_commands into a div on the current page.
The problem is when I submit the form without providing a file for the required file field (intentionally triggering error), form_set_error redirects the user to the menu path originally called to produce the form (sitename-ajax/new-profile), which displays an unrendered ajax response. How do I get the validation response and return it to the target div on the same page – preventing a page redirect? Code pasted below is simplified for reference. Thanks!
In my hook_menu():
$items['sitename-ajax/new-profile'] = array( 'title' => t('New Profile'), 'type' => MENU_CALLBACK, 'page callback' => 'sitename_ajax_new_profile', 'access arguments' => array('access content'), );
Page callback:
function sitename_ajax_new_profile() { $form = sitename_forms_prepare_form('ps_profile'); $output = drupal_render($form); $commands = array(); $commands[] = ajax_command_html('#new-form-target', $output); $page = array('#type' => 'ajax', '#commands' => $commands); ajax_deliver($page); }
Form helper function:
function sitename_forms_prepare_form($type) { global $user; module_load_include('inc', 'node', 'node.pages'); $node = (object) array( 'uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => LANGUAGE_NONE ); $output = drupal_get_form($type . '_node_form', $node); return $output; }