You are here

public function LibraryCheckOutBulkForm::validateForm in Library 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

src/Form/LibraryCheckOutBulkForm.php, line 84

Class

LibraryCheckOutBulkForm
Bulk checkout form.

Namespace

Drupal\library\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) : void {
  parent::validateForm($form, $form_state);
  $data = $form_state
    ->getValues();
  foreach ($data as $key => $item) {
    if (strpos($key, 'item_') !== FALSE && !empty($item)) {
      $libraryItem = LibraryItemHelper::findByBarcode($item);
      if ($libraryItem) {
        $itemState = $libraryItem
          ->get('library_status')->value;
        $action = LibraryAction::load($data['action']);
        if ($action
          ->action() == LibraryAction::CHANGE_TO_AVAILABLE) {
          if ($itemState != LibraryItem::ITEM_UNAVAILABLE) {
            $form_state
              ->setErrorByName('item_' . $key, $this
              ->t('Item @item is already checked in.', [
              '@item' => $item,
            ]));
          }
        }
        if ($action
          ->action() == LibraryAction::CHANGE_TO_UNAVAILABLE) {
          if ($itemState != LibraryItem::ITEM_AVAILABLE) {
            $form_state
              ->setErrorByName('item_' . $key, $this
              ->t('Item @item is already checked out.', [
              '@item' => $item,
            ]));
          }
        }
        if ($libraryItem
          ->get('in_circulation')->value == LibraryItem::REFERENCE_ONLY) {
          $form_state
            ->setErrorByName('item_' . $key, $this
            ->t('Item @item cannot be checked out.', [
            '@item' => $item,
          ]));
        }
      }
    }
  }
}