You are here

public function AddViewsCustomTable::validateForm in Views Custom Table 9.0.x

Same name and namespace in other branches
  1. 8 src/Form/AddViewsCustomTable.php \Drupal\view_custom_table\Form\AddViewsCustomTable::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/AddViewsCustomTable.php, line 205

Class

AddViewsCustomTable
Add views custom table form.

Namespace

Drupal\view_custom_table\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if ($this->step == 1) {
    $databaseName = $form_state
      ->getValue('table_database');
    $table_name = $form_state
      ->getValue('table_name');
    $description = $form_state
      ->getValue('description');
    $connection = Database::getConnection('default', $databaseName);
    if (!$connection
      ->schema()
      ->tableExists($table_name)) {
      $form_state
        ->setErrorByName('table_name', $this
        ->t('@table not found in database @database_name, please check table name, and database again.', [
        '@table' => $table_name,
        '@database_name' => $databaseName,
      ]));
    }
    $config = $this->config
      ->getRawData();
    if (isset($config[$table_name]) && $config[$table_name]['table_database'] == $databaseName) {
      $form_state
        ->setErrorByName('table_name', $this
        ->t("@table is already available for views. If you can't find it, please clear cache and try again.", [
        '@table' => $table_name,
      ]));
    }
    if (strlen($description) > 254) {
      $form_state
        ->setErrorByName('description', $this
        ->t("Description can not be more then 255 letters. Please update it and try again.", [
        '@table' => $table_name,
      ]));
    }
  }
}