form_set_error() redirects to raw ajax response page 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' =&gt; t('New Profile'),
  'type' =&gt; MENU_CALLBACK,
  'page callback' =&gt; 'sitename_ajax_new_profile',
  'access arguments' =&gt; 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' =&gt; 'ajax', '#commands' =&gt; $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' =&gt; $user-&gt;uid,
    'name' =&gt; (isset($user-&gt;name) ? $user-&gt;name : ''),
    'type' =&gt; $type,
    'language' =&gt; LANGUAGE_NONE
  );

  $output = drupal_get_form($type . '_node_form', $node);
  return $output;
}
The API page for form_set_error() says (emphasis added):


  When a validation error is detected, the validator calls form_set_error() to indicate which element needs to be changed and provide an error message. This causes the Form API to not execute the form submit handlers, and instead to re-display the form to the user with the corresponding elements rendered with an 'error' CSS class (shown as red by default).


I reckon that's probably where your problem lies.  Further down the same page is this:


  Multistep forms not wanting to validate the whole form can set #limit_validation_errors on buttons to limit validation errors to only certain elements.


This looks like it might help.