6

How can I get custom Form API validation to highlight a field the same way the failed #required validation does? (See attached graphic)

Note Graphic:

1) (Green Line) Company Name is empty. '#required' Error is noted and field is highlighted red.

2) (Blue Line) Website is incorrect. 'Custom Validation' Error is noted, but field is NOT highlighted.

How can I automatically highlight the 'Website' field on validation fail and remove it upon success?

Currently using: form_set_error('', t('Your website address is incorrectly formatted.'));

See enter image description here

3 Answers 3

2

Have you tried using hook_field_validate()? (edit) I just realized you are not using the new Field API but instead just the Form API. Perhaps, you can build it as a field? If you are familiar with the Form API in d6, you can pick up the Field API in a couple of hours. The Field API is another level of badassness.

i have a custom field that records business hours. I have it set that if a to day is selected there must be a from day. I more or less just implemented the example giving in api.drupal.org.

*  Implementation of hook_field_validate()
*/
function hours_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if ($item['end_day'] != '--' && $item['start_day'] == '--') {
      $errors[$field['field_name']][$langcode][$delta][] = array( 
        'error' => t('A from day needs to be choosen if a to day is selected. If they are the same -- forexample, Monday and Monday, the whole line when viewed will only be for Monday.'),
      );
    }
  }
}
1
  • This is going at the problem in quite a roundabout way. The Form API has standard ways of highlighting a field that failed validation. Commented Jan 30, 2013 at 16:07
3

As the first parameter to form_set_error(), pass the name of the field to be highlighted. If it's a nested structure, pass it with square brackets, like so:

form_set_error('[your][nested][field]', 'Your Error Message');

There's a convenience wrapper around this, form_error, which takes the element instead of the string, like so:

form_error($form['your_field'], 'Your Error Message');

Also see the docs to form_set_error() (d7).

2
  • Unfortunately your suggestions did not work for me. Neither form_error or form_set_error worked in highlighting the error field. I do get the error message, though. I am using hook_node_view and drupal_get_form to add a form to a node. Maybe this is why? Oh well
    – user785179
    Commented Jan 29, 2013 at 22:34
  • @user785179 - this must be some unrelated problem. It sounds like the FAPI element you mean to work with is not there, or is incomplete, or whatever. I recommend setting a breakpoint on the call to form(_set)_error and examine the variables. Note that form_error really only wraps form_set_error, all the former does is figure out the first parameter to the latter from the FAPI array. Commented Jan 30, 2013 at 16:02
2

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'.

Copied from form_set_error() field not highlited red

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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