0

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(&$form, &$form_state, $form_id) {
  if ($form_id=='webform_client_form_1') {
    $form['#validate'][]='mymodule_form_validate';
    return $form;
  }
}

function mymodule_form_validate($form, &$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?

  • Welcome to Drupal Answers! I removed the second question since it was a different, unrelated question, that is probably already asked. What do you mean by adding the "error" class? You would not need to add a custom validation handler to verify the form field is not empty. Just mark the form field as requested, and the users would be requested to enter a value. – kiamlaluno Mar 3 '18 at 10:32
  • As side note, hook_form_alter() doesn't need to return the form. It is passed as reference. – kiamlaluno Mar 3 '18 at 10:33
  • Thank you. I have an other test then not empty.. I want to make the border of input red.. for requested input , the module add class error if field is empty.. I want to add the same class if my test is false. – A.Sana Mar 3 '18 at 11:13
  • $form['aux']['#attributes']['class'][] = 'error'; doesn't work! – A.Sana Mar 5 '18 at 9:45
  • $form['submitted']['aux']['#attributes']['class'][] = 'error'; doesn't work also! Any idea please? Thank you – A.Sana Mar 5 '18 at 13:41
0

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.

| improve this answer | |
  • Thank you! My test is correct because form_set_error works.. And I checked also my input's name and it's correct too! – A.Sana Mar 6 '18 at 8:57
  • @A.Sana Great, that's a good sign. So when you say form_set_error works what do you mean? – sagannotcarl Mar 6 '18 at 22:42
  • 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'; – sagannotcarl Mar 6 '18 at 22:57
  • Thanks @sagannotcarl, when I say form_set_error works, I want to say that my test is true.. Now what do you mean by "complete form" ? Is not 'submitted' ? Thank you – A.Sana Mar 7 '18 at 8:17
  • That's right. I tried that and $form_state['completed form'] worked for me. Note that it's $form_state and not $form as well. – sagannotcarl Mar 7 '18 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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