2

When validating a field manually from my form a error is displayed but the field doesn't get red. For the first parameter i used the name of the input element. DRUPAL 7

if($form_state['values']['panes']['webform_nid31'][0]['contact_person'] == '') {
    form_set_error('panes[webform_nid31][0][contact_person]', t('You must select a name for this group of settings.'));
  }
7

If your form element is defined like that:

$form['panes']['webform_nid31'][0]['contact_person'] = array( ... );

your form_set_error call should look like that:

form_set_error('panes][webform_nid31][0][contact_person',
                t('You must select a name for this group of settings.'));

You need to set full path to element, without outside [ and ]. You could have many "contact_person" fields in one form, just in different branches of a form tree. Using only last index would made setting errors ambiguous, thus it's not supported. At least officially. Might work, but only as an undocumented feature.

Formatting this string is described in API:

Parameters

$name: The name of the form element. If the #parents property of your form element is array('foo', 'bar', 'baz') then you may set an error on 'foo' or 'foo][bar][baz'. Setting an error on 'foo' sets an error for every element where the #parents array starts with 'foo'.

Last but not least, actual output is defined in theme, it might well be something other than red border if theme author decided so.

  • 1
    You can infer the form structure from the values array in the question: $form_state['values']['panes']['webform_nid31'][0]['contact_person'] == $form['panes']['webform_nid31'][0]['contact_person']. On another note, have you seen any official reference for this? I only know to do this because I was told about it by another developer a few years back, I haven't ever seen it written down – Clive Jul 5 '13 at 15:02
  • @Clive In theory I know that, simply after 9 hours of working at one particularly irritating form, first day on heart drugs, my mind isn't as bright as it should be. Sorry again. – Mołot Jul 5 '13 at 15:04
  • you solution works but it should also work by giving the name of the element as first parameter. As you are saying this should give all elements with that name a red border. But even though that might nog be what I want as an end result it should work. – DD dev Jul 5 '13 at 15:05
  • @Clive official reference added – Mołot Jul 5 '13 at 15:07
  • 1
    @Mołot Guess I didn't look too hard ;) – Clive Jul 5 '13 at 15: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.