You are here

function merci_line_item_ui_form_validate in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

Form API validate callback for the entity_type form

1 string reference to 'merci_line_item_ui_form_validate'
merci_line_item_form in merci_line_item/merci_line_item_ui.pages.inc
Form callback: create or edit a entity_type.

File

merci_line_item/merci_line_item_ui.pages.inc, line 101

Code

function merci_line_item_ui_form_validate(&$form, &$form_state) {
  $entity_type = $form_state['entity_type'];
  $entity = $form_state[$entity_type];
  $account = user_load_by_name($form_state['values']['owner']);

  // Validate the "authored by" field.
  if (!$account) {

    // The use of empty() is mandatory in the context of usernames
    // as the empty string denotes the anonymous user. In case we
    // are dealing with an anonymous user we set the user ID to 0.
    form_set_error('name', t('The username %name does not exist.', array(
      '%name' => $form_state['values']['owner'],
    )));
  }
  $entity->uid = $account->uid;

  // Notify field widgets to validate their data.
  field_attach_form_validate($entity_type, $entity, $form, $form_state);

  // Do no validation if their errors from the main validation function.
  if (form_get_errors()) {
    return;
  }

  // Determine if they are any conflicts.
  try {
    merci_line_item_validate($entity);
  } catch (MerciException $e) {
    foreach ($e
      ->getData()
      ->getErrors() as $delta => $errors) {
      $msg = array();
      if (array_key_exists(MERCI_ERROR_TOO_MANY, $errors)) {
        $msg[] = $errors[MERCI_ERROR_TOO_MANY];
      }
      elseif (array_key_exists(MERCI_ERROR_CONFLICT, $errors)) {
        foreach ($errors[MERCI_ERROR_CONFLICT] as $date_start => $message) {
          $msg[] = $message;
        }
      }
      $parents_path = implode('][', array(
        MERCI_RESOURCE_REFERENCE,
        'und',
        $delta,
        'target_id',
      ));
      form_set_error($parents_path, t('!errors', array(
        '!errors' => implode('<br> ', $msg),
      )));
    }
  }
}