#ajax form_set_error I'm using the below code to query a webform field's value against database, and if the value exists, I want to use form_set_error, however I can't, since it requires rewriting the page. I have found this post but it doesn't have an answer, hence my question.

function tubitekuz_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch($form_id) {
    case 'webform_client_form_97':
      global $user;
      if ($user-&gt;uid &gt; 0) {
        $form['submitted']['kullanici_adi']['#access'] = FALSE;
      }
      $form['submitted']['kullanici_adi']['#ajax'] = array(
        'event' =&gt; 'blur',
        'callback' =&gt; '_tubitekuz_check_username',
      );
      break;
    case 'contact_site_form':
      $form['#submit'] = array('_tubitekuz_show_message');
      break;
    default:
      break;
  }
}

function _tubitekuz_check_username($form, &amp;$form_state) {
  $username= $form_state['values']['submitted']['kullanici_adi'];
  $exists = db_query('SELECT name FROM {users} WHERE name = :entered', array(':entered' =&gt; $username))-&gt;fetchField();
  if ($exists) {
    $commands = array();
    $commands[] = drupal_set_message('Kullanıcı adı kayıtlı, lütfen başka bir kullanıcı adı seçin');
    return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);
  }
}


How do I go about that?

PS: drupal_set_message does its thing only when I refresh the page, whereas form_set_error doesn't do at all.

EDIT: I tried to replace the above with the below code:

function _tubitekuz_check_username($form, &amp;$form_state) {
  $username= $form_state['values']['submitted']['kullanici_adi'];
  $exists = db_query('SELECT name FROM {users} WHERE name = :entered', array(':entered' =&gt; $username))-&gt;fetchField();
  if ($exists) {
    drupal_add_js(drupal_get_path('module', 'tubitekuz') . '/js/tubitekuz.js');
    drupal_add_css(drupal_get_path('module', 'tubitekuz') . '/css/tubitekuz.css');
  }
}


and in tubitekuz.js I am doing

$(document).ready(function() {
    var username=jQuery('#edit-submitted-kullanici-adi');
    username.addClass('err');
    setTimeout(function() {
      username.removeClass('err');
      }, 3000);
});


and in tubitekuz.css I have the below:

.err {
  border-color:red;
  border-width:2px;
}


however, CSS rules are not applied even though class is added. What is the cause of this? All I'm trying to do is use form_set_error with #ajax
Your code needs to be adjusted to something like:

function tubitekuz_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch($form_id) {
    case 'webform_client_form_97':
      global $user;
      if ($user-&gt;uid &gt; 0) {
        $form['submitted']['kullanici_adi']['#access'] = FALSE;
      }
      $form['submitted']['kullanici_adi'] += array(
        '#prefix' =&gt; '&lt;div id="kullanici-adi-container"&gt;',
        '#ajax' =&gt; array(
          'callback' =&gt; 'kullanici_adi_form_login',
          'wrapper' =&gt; 'kullanici-adi-container',
        ),
        '#suffix' =&gt; '&lt;/div&gt;',
      );

      // Check the form state to see if an e-mail address has been specified.
      if (!empty($form_state['values']['kullanici_adi'])) {
        $mail = trim($form_state['values']['kullanici_adi']);

        if ($account = user_load_by_mail($mail)) {
          form_set_error('submitted][kullanici_adi', 'Kullanıcı adı kayıtlı, lütfen başka bir kullanıcı adı seçin');
        }
      }
      break;
  }
}

function kullanici_adi_form_login($form, $form_state) {
  return $form['submitted']['kullanici_adi'];
}


You will probably have to play around with some of the arrays, etc. to get the correct values, etc.