I have a form with a bunch of required fields that all have the same callback. Here's a single field example:

$form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'callback_show_buttons',
      'wrapper' => 'formwrapper',

formwrapper is a wrapper around the entire form:

$form['#prefix'] = '<div id="formwrapper">';
$form['#suffix'] = '</div>';

Here's the callback, inspired by this guy's code:

function callback_show_buttons($form, &$form_state) {
    $allajaxchanges = array( '#type' => 'ajax', '#commands' => array() );

    if ( form_get_errors() ) {
        $form_state['rebuild'] = TRUE;
        $allajaxchanges['#commands'][] = ajax_command_replace( "#formwrapper", render($form) );

 return $allajaxchanges;

If I inspect form_get_errors(), it is always NULL, no matter if I validate the form in code or not. drupal_get_messages() is also completely empty, and theme('status_messages') also has nothing in it (these solutions were suggested here). These methods do return something if I submit the form in a non-AJAX way.

Anyone have any idea how to check if a form has errors in it if these don't work?

  • Did you ever find a solution for this?
    – rooby
    Commented Apr 23, 2015 at 1:02

2 Answers 2


I found a way to make this work.

I haven't fully investigated this for issues yet but it works with and without javascript enabled.

My use case was only running my validation if other validation passed:

 * Custom validation function for some form.
function MODULE_NAME_custom_form_validate(&$form, &$form_state) {
  if (isset($form_state['storage']['errors']) && !empty($form_state['storage']['errors'])) {
  // Do our extra validation here since other validation passed.

You need to add drupal_validate_form('give_your_form_id', $form, $form_state); on ajax callback function and return return $form["wrapper"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.