1

I've a custom field, and I'm trying to write the validation for the field values. however when a particular field causes the failure, the whole fielset is marked in red. I only want the specific field element to be highlighted, and in the case of Unlimited I only want the specific entry highlighted.

here is the code I have so far. but the whole fieldset is marked in red when ever a validation fails.

   function prodlist_field_widget_form(&$form, &$form_state, $field, $instance, $langtype, $items, $delta, $element) {
      $base = $element;

      if ($instance['widget']['type'] == 'prodlist_text') {
        $element['#element_validate'] = array('_prodlist_text_validate');


        $element['prod_type'] = array(
          '#type' => 'select',
          '#title' => t('Product Type'),
          '#default_value' => isset($items[$delta]['prod_type']) ? $items[$delta]['prod_type'] : NULL,
          '#options' => type_options(),
          ) + $base;

        $element['prod_description'] = array(
          '#type' => 'select',
          '#title' => t('Description'),
          '#default_value' => isset($items[$delta]['prod_description']) ? $items[$delta]['prod_description'] : NULL,
          '#options' => description_options(),
          ) + $base;

        $element['order_amount'] = array(
          '#type' => 'textfield',
          '#title' => t('Amount'),
          '#default_value' => isset($items[$delta]['order_amount']) ? $items[$delta]['order_amount'] : NULL) + $base;
      }
      return $element;

    }


function _prodlist_text_validate($element, &$form_state) {

  if ($form_state['complete form']['#form_id'] == 'field_ui_field_edit_form') {
    return ;
  }

  $values = $form_state['values'];
  $language = $values['language'];
  $field_name = $element['#field_name'];
  $field_name_error_box = $field_name;

  foreach ($values[$field_name][$language] as $delta => $item) {

    if (isset($item['order_amount'])) {

      if (is_numeric($item['order_amount'])) {
        if($item['prod_description'] != 'no order' && $item['order_amount'] <= 0 ){
          form_set_error($field_name_error_box, t('Order amount has to be more then 0'));
        } else if ($item['prod_description'] == 'no order' && $item['order_amount'] > 0) {
          form_set_error($field_name_error_box, t('You have created an order amount without selecting a product.'));
        }


      } else {
        form_set_error($field_name_error_box, t('Order amount has to be numeric'));
      }
   }

  }

}

I'm trying to set the variable $field_name_error_box = $field_name; to the correct value but it either lights up the whole form section or nothing at all.

I've tried the following, and lots of other variants without luck.

$field_name_error_box = $field_name . 'order_amount';
1

You must use a field name like this: 'parent][element'

In your case,

$field_name_error_box = $field_name . '][order_amount';

should work.

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