You are here

function entityreference_rendered_widget_validate in Entityreference Rendered Widget 7

Validate checkboxes elements.

1 string reference to 'entityreference_rendered_widget_validate'
entityreference_rendered_widget_field_widget_form in ./entityreference_rendered_widget.module
Implements hook_field_widget_form().

File

./entityreference_rendered_widget.module, line 263
Entityreference Rendered Widget module.

Code

function entityreference_rendered_widget_validate($element, &$form_state, $form) {
  array_pop($element['#parents']);
  $input = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  $langcode = $element['#language'];
  $field_name = $element['#field_name'];
  $field = field_info_field($field_name);
  if ($field['cardinality'] == 1) {
    $input[$langcode] = array(
      $input[$langcode][0],
    );
    form_set_value($element, $input, $form_state);
  }
  else {

    // Initial value.
    $empty = TRUE;
    foreach ($input[$langcode] as $key => $values) {
      if (!empty($values['target_id'])) {
        $empty = FALSE;
      }
      else {
        $input[$langcode][$key]['target_id'] = NULL;
      }
    }

    // Verify if required field.
    if ($element['#required']) {

      // Message if field is empty.
      if ($empty) {
        $array_error = array(
          '@field' => $element['#title'],
        );
        form_set_error($element['#field_name'], t('@field is required.', $array_error));
      }
    }
    form_set_value($element, $input, $form_state);
  }
}