0

I have the following code but the custom validator is not being fired when I add a file to the file field.

If the file isn't a csv then I get an error message but if I upload a csv then the second validator never fires.

Currently it's not checking anything just sending an error back so I can get it working.

<?php

namespace Drupal\mymodule_alias_manager\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\FileInterface;

/**
 * Provides a mymodule_alias_manager form.
 */
class mymoduleAliasManagerImportForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'mymodule_alias_manager_mymodule_alias_manager_import';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['path_alias_csv'] = [
      '#type' => 'managed_file',
      '#title' => $this->t('path alias csv'),
      '#description' => t('upload a csv file containing 3 columns with an entry id, /alias, the /node/id and the langcode.The first line being the array header is not considered'),
      '#upload_location' => 'private://mymodule_alias_maanger_import',
      '#upload_validators' => [
        'file_validate_extensions' => ['csv'],
        'validate_alias_csv' => [],
      ],
    ];

    $form['results'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'accommodation',
        ],
      ],
    ];

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Import aliases'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (empty($form_state->getValue('path_alias_csv'))) {
      $form_state->setErrorByName('path_alias_csv', $this->t('File should not be empty'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if($valid){
      $this->messenger()->addStatus($this->t('The message has been sent.'));
    }

  }



    /**
   * Validate test.
   *
   * @param \Drupal\file\FileInterface $file
   *   File object.
   *
   * @return array
   *   Errors array.
   */
  public function validate_alias_csv(FileInterface $file, $astring) {
    $errors = [];

    $errors[] = t("The file has an error");

    return $errors;
  }

}
2
  • I've added the function into the module file and that works, so can I keep the function in the form class by changing how it's specified in "#upload_validators" ? Jun 22 at 14:27
  • 1
    I don't think you can. If I recall correctly, there isn't a way to call upload validators in classes, they need to be global functions, and therefore in the .module file.
    – Jaypan
    Jun 22 at 15:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.