You are here

public function WebformOptionsCustomForm::validateForm in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_options_custom/src/WebformOptionsCustomForm.php \Drupal\webform_options_custom\WebformOptionsCustomForm::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

modules/webform_options_custom/src/WebformOptionsCustomForm.php, line 412

Class

WebformOptionsCustomForm
Provides a form to set webform options custom.

Namespace

Drupal\webform_options_custom

Code

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

  /** @var \Drupal\webform_options_custom\WebformOptionsCustomInterface $webform_options_custom */
  $webform_options_custom = $this
    ->getEntity();
  $this
    ->copyFormValuesToEntity($webform_options_custom, $form, $form_state);

  // Make sure the URL exists.
  $url = $webform_options_custom
    ->getUrl();
  if ($url) {
    $file_exists = FALSE;
    try {
      $response = $this->httpClient
        ->get($url);
      $file_exists = $response
        ->getStatusCode() === 200;
    } catch (\RangeException $exception) {

      // Do nothing.
    }
    if (!$file_exists) {
      $t_args = [
        '%url' => $form_state
          ->getValue('url'),
      ];
      $form_state
        ->setErrorByName('url', $this
        ->t('HTML/SVG file URL or path %url not found.', $t_args));
    }
  }
}