You are here

public function AdminSettingsForm::validateForm in PDF using mPDF 8

Same name and namespace in other branches
  1. 8.2 src/Form/AdminSettingsForm.php \Drupal\pdf_using_mpdf\Form\AdminSettingsForm::validateForm()

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/AdminSettingsForm.php, line 349

Class

AdminSettingsForm
Admin settings form.

Namespace

Drupal\pdf_using_mpdf\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if (!is_numeric($values['pdf_font_size']) || $values['pdf_font_size'] < 1) {
    $form_state
      ->setErrorByName('pdf_font_size', $this
      ->t('Font size should be numeric and greater than 1.'));
  }
  if (!is_numeric($values['margin_top']) || $values['margin_top'] <= 0) {
    $form_state
      ->setErrorByName('margin_top', $this
      ->t('PDF top margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['margin_right']) || $values['margin_right'] <= 0) {
    $form_state
      ->setErrorByName('margin_right', $this
      ->t('PDF right margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['margin_bottom']) || $values['margin_bottom'] <= 0) {
    $form_state
      ->setErrorByName('margin_bottom', $this
      ->t('PDF bottom margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['margin_left']) || $values['margin_left'] <= 0) {
    $form_state
      ->setErrorByName('margin_left', $this
      ->t('PDF left margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['margin_header']) || $values['margin_header'] <= 0) {
    $form_state
      ->setErrorByName('margin_header', $this
      ->t('PDF header margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['margin_footer']) || $values['margin_footer'] <= 0) {
    $form_state
      ->setErrorByName('margin_footer', $this
      ->t('PDF footer margin should be numeric and greater than -1.'));
  }
  if (!is_numeric($values['dpi']) || $values['dpi'] < 0) {
    $form_state
      ->setErrorByName('dpi', $this
      ->t('Document DPI should be numeric and greater than 0.'));
  }
  if (!is_numeric($values['img_dpi']) || $values['img_dpi'] < 0) {
    $form_state
      ->setErrorByName('img_dpi', $this
      ->t('Image DPI should be numeric and greater than 0.'));
  }

  // Change watermark image status to permanent.
  if (!empty($values['watermark_image'])) {
    $file = File::load($values['watermark_image'][0]);
    if (!$file
      ->isPermanent()) {
      $file
        ->setPermanent();
      $file
        ->save();
      $file_usage = \Drupal::service('file.usage');
      $file_usage
        ->add($file, 'pdf_using_mpdf', 'user', \Drupal::currentUser()
        ->id());
    }
  }

  // Validate custom stylesheet.
  if (!empty($values['pdf_css_file'])) {
    $path = DRUPAL_ROOT . '/' . $values['pdf_css_file'];
    if (!file_exists($path)) {
      $form_state
        ->setErrorByName('pdf_css_file', $this
        ->t('Stylesheet not found in path: @path', [
        '@path' => $path,
      ]));
    }
    if ($target_path = pathinfo($path)) {
      if ($target_path['extension'] != 'css') {
        $form_state
          ->setErrorByName('pdf_css_file', $this
          ->t('*.css file extension is needed.'));
      }
    }
  }
}