You are here

public function SettingsForm::validateForm in Sophron 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/SettingsForm.php, line 270

Class

SettingsForm
Main Sophron settings admin form.

Namespace

Drupal\sophron\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Custom map class.
  if ($form_state
    ->getValue('map_option') == MimeMapManagerInterface::CUSTOM_MAP && !$this->mimeMapManager
    ->isMapClassValid($form_state
    ->getValue('map_class'))) {
    $form_state
      ->setErrorByName('map_class', $this
      ->t("The map class is invalid. Make sure the selected class is an extension of \\FileEye\\MimeMap\\Map\\AbstractMap."));
  }

  // Mapping commands.
  if ($form_state
    ->getValue('map_commands') !== '') {
    try {
      $map_commands = Yaml::parse($form_state
        ->getValue('map_commands'));
      $data = $this->configFactory
        ->get('sophron.settings')
        ->get();
      $data['map_commands'] = $map_commands;
      $schema_errors = $this
        ->checkConfigSchema($this->typedConfig, 'sophron.settings', $data);
      if (is_array($schema_errors)) {
        $fail_items = [];
        foreach ($schema_errors as $key => $value) {
          $matches = [];
          if (preg_match('/sophron\\.settings\\:map\\_commands\\.(\\d+)/', $key, $matches)) {
            $item = (int) $matches[1] + 1;
            $fail_items[$item] = $item;
          }
        }
        $form_state
          ->setErrorByName('map_commands', $this
          ->t("The items at line(s) @lines are wrongly typed. Make sure they follow the pattern '- [method, [arg1, ..., argN]]'.", [
          '@lines' => implode(', ', $fail_items),
        ]));
      }
    } catch (\Exception $e) {
      $form_state
        ->setErrorByName('map_commands', $this
        ->t("YAML syntax error: @error", [
        '@error' => $e
          ->getMessage(),
      ]));
    }
  }
}