0

How can I add the red error border to a webform component programmatically?

I am doing so custom form validation when the form submits and I need to set the red border on under rules very specific to my module.

Currently, I am doing the following:

form_set_error('email_address', 'Please supply matching email addresses');

It adds an error message and stops the form from being submitted but it does not highlight the form field I am validating. That makes it difficult for the users to see the problem area at a glance

2

You can use Webform Validation. This module includes a lot of validations for webform elements.

Alternatively, you can code your own validation yous adding your validation function to the desired forms. Then use form_set_error to mark wrong element with red border.

Webform puts the fields inside a group, so if you need to mark a field as not valid you must provide the full hierarchy.

For example, for this field in $form:

$form['submitted']['form_contact_data']['form_contact_email'] = array(
...
);

You can mark it as erroneous with:

form_set_error('submitted][form_contact_data][form_contact_email', 'Error message');

In this example the filed is inside two groups: the first, 'submitted' is the main Webform group. The second, 'form_contact_data', is a field group created where I've placed contact data fields for this webform.

  • Thanks for your answer. I am already using form_set_error. I amended my question so you can see how I am using that function – sisko Sep 18 '12 at 11:27
  • You probably aren't passing the right first argument to form_set_error, the one that indicates wich element to highlight. You have to provide the hierarchy for the element if it's inside a fieldset. And checking Webform Validation module it seems that it prefi every webform element with 'submitted][', so in your case you may use 'submitted][email_address' (Drupal 7). – sanzante Sep 18 '12 at 11:31
  • Which is what I though. But, I have confirmed and copied the form_key from the webform_component database table...unless I'm not supposed to use form_key, I can't see what I'm doing wrong. I will look into your suggestion – sisko Sep 18 '12 at 11:34
  • Updated answer with example – sanzante Mar 10 '15 at 8:08

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.