2

Is it possible to highlight only one field with form_set_error when this field belongs to a multiple value field. Let's say I have a field name field_videos which accepts multiples values. After running validation, only video #2 isn't valid :

form_set_error('field_videos', t('Video URL not valid'));

How can I tell drupal to highlight only field #2 ?

Thanks

6

I solved this by using the delta of the field with the error. In this example, field_acceptable_domains is a multi-value text field and I'm using valid_url to validate each value.

// This is a multi-value field, so we need to loop through the values.
foreach ($form_state['values']['field_acceptable_domains'][LANGUAGE_NONE] as $delta => $url) {
  if (!valid_url($url['value']) && !empty($url['value'])) {
    form_set_error('field_acceptable_domains][' . LANGUAGE_NONE . '][' . $delta, t('The domain %domain is not valid.', array('%domain' => $url['value'])));
  }
}

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.