How do I add the "error" class when a form validation fails? I'm working with Drupal 7.56 and I am using the Webform module to create a custom form.

I used hook_form_alter() in a custom module to add a validation test. 

function mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if ($form_id=='webform_client_form_1') {
    $form['#validate'][]='mymodule_form_validate';
    return $form;
  }
}

function mymodule_form_validate($form, &amp;$form_state) {
  $aux = $form_state['values']['submitted']['aux'];
  if ($aux != '') {
    if( /* my test */ ) {
      $text = t('Please check your input');
      form_set_error('aux', $text);
    }
  }
}


How can I add the error class when my test fails?
First try removing your if test and see if you can get the form_set_error to trigger. That way you can make sure your test is correct. You should be able to call form_set_error in a validation function and it will work. 

If that isn't the problem then make sure the name of your field is correct.

To answer the question about simply adding a class in form validation you can use the following:
$form_state['complete form']['aux']['#attributes']['class'][] = 'error';

It won't add to the input itself but to the field wrapper and you can style it from there. But I think figuring out why form_set_error isn't behaving how you want is your best bet. That should really be adding the error class.